HopperBins: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Zuka
No edit summary
 
>JulienDethurens
Instance::Remove is deprecated; changing to Instance::Destroy.
 
(42 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{CatUp|Tutorials}}
__TOC__
==What is a HopperBin?==
==What is a HopperBin?==
HopperBins are considered old now. They were the first ways to make a weapon, a utility tool, or anything you could really think of. They're mainly used for getting input from the player, and doing something with that input.
HopperBins are considered old now. They were the first ways to make a weapon, a utility tool, or anything you could really think of. They are mainly used for getting input from the player, and doing something with that input.


==Some useful things to know==
==Special Events==
You should know the events of a HopperBin. The special ones are as follows:
The special events of a HopperBin are as follows:


#Selected
#Selected
#Deselected
#Deselected


==What to do with them?==
==Important events and properties==
Well, the Selected and Deselected events have an argument, called a MOUSE object. Here are it's important events and properties.
The Selected and Deselected events have an argument, called a MOUSE object. Here are its important events and properties.


'''Events'''
'''Events'''
#Button1Down
#Button1Down
#Button1Up
#Button1Up
#Button2Down -- current un-operational
#KeyDown
#Button2Up -- also non-working
#KeyUp
#WheelForward -- non-working
#Move
#WheelBackward -- non-working
<!-- I deleted those non-working events because they don't really benefit users. -->
'''Properties'''
'''Properties'''
#Hit -- CFrame value
#Hit -- CFrame value
Line 28: Line 31:
#Icon -- the cursor image path
#Icon -- the cursor image path


==What do I do with that stuff?==
==What do I do with it?==
You can use it to do almost anything! You can spawn a projectile when Button1Down is fired, have it spawn in the direction the mouse is from your head, and move in the direction of the mouse.
You can spawn a projectile when Button1Down is fired, have it spawn in the direction the mouse is from your head, and move in the direction of the mouse, for example.


Here's an example of a Boulder Summoner.
Here's an example of a Boulder Summoner:
 
1) Insert / Object / HopperBin into the StarterPack.<br>
2) Insert / Object / Script into the HopperBin you just created.<br>
3) Insert the following into the script file you just created:<br>


<pre>
<pre>
Line 56: Line 63:
</pre>
</pre>


Thats a simple thing to spawn a boulder wherever you click. It also gets rid of the boulder after a set amount of time.
4) Test in Solo mode.  


==Okay, what else?==
That is a simple thing to spawn a boulder wherever you click. It also gets rid of the boulder after a set amount of time.
Not much, you just need to understand how things work in ROBLOX Lua. There are PLENTY of different weapons and HopperBins to learn off of.


==More examples==
==Simple Toggle Utility==
This tutorial wouldn't be complete without some decent examples.
Insert a Brick in the Workspace.  Insert a BoolValue into that Brick.  Name that BoolValue "OnOff".
 
Just as before, you have a Hopperbin object in the StarterPack, and a script in the HopperBin. Insert the following into that script:
===Simple Toggle Utility===
Assuming you have a brick named "Toggle", and it has a BoolValue in it named "On/Off", you can use this.


<pre>
<pre>
Line 78: Line 82:
if targ[valueName].Value == true then
if targ[valueName].Value == true then
targ[valueName].Value = false
targ[valueName].Value = false
print("one")
else
else
targ[valueName].Value = true
targ[valueName].Value = true
print("two")
end
end
end
end
Line 93: Line 99:
</pre>
</pre>


Pretty cool, right? You can easily manipulate this script to work for anything.
Upon clicking the mouse on the brick in Solo Mode, you can see the Output toggle between "one" and "two".
You can easily manipulate this script to work for anything.
 
==Mouse Without HopperBin==
 
===Setting up the HopperBin===
You need a [[RBX.lua.HopperBin (Object)|HopperBin object]]. This is what the player selects to activate the whole system. 
 
You also need two scripts.  Here is how they should look in the Explorer:
 
<pre>
[-] HopperBin
ConnectionScript
ControlScript
</pre>
 
The script named ConnectionScript is the one that sets it all up, and links the mouse to some values. The HopperBin gets removed, the two scripts moved, but the mouse is still active until you select a different HopperBin.
 
=== ConnectionScript ===
Here is what goes in "ConnectionScript":
 
<pre>
function onSelected(mouse)
local m = Instance.new("CFrameValue")
m.Parent = script.Parent.Parent.Parent
m.Name = "MouseCFrame"
local b = Instance.new("BoolValue")
b.Parent = script.Parent.Parent.Parent
b.Name = "Button1Down"
local k = Instance.new("StringValue")
k.Parent = script.Parent.Parent.Parent
k.Name = "KeyPressed"
local t = Instance.new("ObjectValue")
t.Parent = script.Parent.Parent.Parent
t.Name = "MouseTarget"
mouse.Move:connect(function() m.Value = mouse.Hit end)
mouse.Button1Down:connect(function() b.Value = true end)
mouse.Button1Up:connect(function() b.Value = false end)
mouse.KeyDown:connect(function(key) k.Value = key k.Value = "" end)
mouse.Changed:connect(function(property)
if (property == "Target") then
t.Value = mouse.Target
end
end)
local hb = script.Parent
script.Parent = hb.Parent
hb.ControlScript.Parent = script.Parent
hb:Destroy()
end
 
script.Parent.Selected:connect(onSelected)
</pre>
 
=== ControlScript ===
 
Here is the ControlScript:
 
<pre>
 
while true do
if script.Parent.Name == "Backpack" then  -- this is to pause the script until it gets moved, no bugging up.
break -- this stops the while loop. 'breaking' it
end
wait()
end
 
function newText(text)
local nm = Instance.new("Hint")
nm.Parent = p
nm.Text = text
end
 
function onKeyDown(key)
if script.Parent.Parent.Character==nil then return end
local key = string.lower(key)
end
 
function onButton1Down()
if script.Parent.Parent.Character==nil then return end
end
 
function onButton1Up()
if script.Parent.Parent.Character==nil then return end
end
 
function onMove()
if script.Parent.Parent.Character==nil then return end
end
 
script.Parent.Parent.KeyPressed.Changed:connect(function() onKeyDown(script.Parent.Parent.KeyPressed.Value) end)
script.Parent.Parent.MouseCFrame.Changed:connect(function() onMove() end)
script.Parent.Parent.Button1Down.Changed:connect(function()
if script.Parent.Parent.Button1Down.Value == true then
onButton1Down()
else
onButton1Up()
end
end)</pre>
 
==How To Make onKeyDown Scripts with HopperBins==
 
As above, you will need a HopperBin in the StarterPack, and an empty Script in the HopperBin.
 
==The Basic Script==
You will need to add different parts to the basic script to make it work. It '''must''' have :
 
*An '''"if key == "(key)" then"''' line.
*Sometimes an '''or''' line. There '''can''' be more than one.
 
Here is the unedited, basic script:
 
<pre>
function onKeyDown(key)
key:lower()
--*********************--
--*********************--
--*********************--
--*********************--
--*********************--
end
 
 
function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
 
script.Parent.Selected:connect(onSelected)
</pre>
 
Put the '''if''' line where the commented stars are. The lines where the stars are, are also where the stuff happens. For example, if you press "f", you die. So the lines would be
 
<pre>
if key == "f" then
script.Parent.Parent.Parent.Character.Humanoid.Health = 0
end
</pre>
 
*This finds the script's parent [your HopperBin], its parent [your backpack] its parent [your player] then your Character [you in the workspace] then your Humanoid, and finally its health.
*You can also change '''onKeyDown(key)''' to '''onButton1Down(mouse)'''. Make sure you change the '''connection''' line as well.
 
A line with '''or''' in it would look like this :
 
<pre>
if key == "b" or "h" or "k" then --Etc.
--The things you want to happen go here.
end
</pre>
 
An example of a script that kills the player via random explosion would be :
 
<pre>
e=Instance.new("Explosion")
function onKeyDown(key)
key:lower()
if key == "f" then
e.Position = script.Parent.Parent.Parent.Character.Head.Position
e.Parent = game.Workspace
e.BlastRadius = 10
e.BlastPressure = 100000
end
 
end
 
 
function onSelected(mouse)
mouse.KeyDown:connect(onKeyDown)
end
 
script.Parent.Selected:connect(onSelected)
</pre>
 
To make sure that the script works, it is recommended to use Output. It will tell you exactly what you messed up on, and even what line.
 


''''Ever helpful,''''
[[Category:Scripting Tutorials]]
--[[User:Zuka|Zuka]] 22:21, 11 April 2008 (CDT)--

Latest revision as of 03:32, 23 January 2012

What is a HopperBin?

HopperBins are considered old now. They were the first ways to make a weapon, a utility tool, or anything you could really think of. They are mainly used for getting input from the player, and doing something with that input.

Special Events

The special events of a HopperBin are as follows:

  1. Selected
  2. Deselected

Important events and properties

The Selected and Deselected events have an argument, called a MOUSE object. Here are its important events and properties.

Events

  1. Button1Down
  2. Button1Up
  3. KeyDown
  4. KeyUp
  5. Move

Properties

  1. Hit -- CFrame value
  2. Target -- object value
  3. X -- mouse's X position on the screen.
  4. Y -- mouse's Y position on the screen.
  5. ViewSizeX -- your screen's X size.
  6. ViewSizeY -- your screen's Y size.
  7. Origin -- CFrame value
  8. Icon -- the cursor image path

What do I do with it?

You can spawn a projectile when Button1Down is fired, have it spawn in the direction the mouse is from your head, and move in the direction of the mouse, for example.

Here's an example of a Boulder Summoner:

1) Insert / Object / HopperBin into the StarterPack.
2) Insert / Object / Script into the HopperBin you just created.
3) Insert the following into the script file you just created:

lifetime = 5 -- how long the boulder stays there. 5 is a good value.

function onButton1Down(mouse)
	local b = Instance.new("Part")
	b.BrickColor = BrickColor.new(217)
	b.Position = mouse.Hit.p
	b.Size = Vector3.new(5, 5, 5)
	b.Shape = 0
	b.Parent = game.Workspace
	b.Velocity = (script.Parent.Parent.Parent.Character.Head.Position - mouse.Hit.p).unit * -50 + Vector3.new(0, 10, 0)
	-- pushes it away from you, and pops it up a bit, making it seem to come out of the ground.
	game:GetService("Debris"):AddItem(b, lifetime)
end

function onSelected(mouse)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end) -- Button1Down doesn't come with a mouse, seeing as its
	-- an event of the mouse itself. We need to give the function the mouse object to work off of.
end

script.Parent.Selected:connect(onSelected) -- Selected comes with a mouse.

4) Test in Solo mode.

That is a simple thing to spawn a boulder wherever you click. It also gets rid of the boulder after a set amount of time.

Simple Toggle Utility

Insert a Brick in the Workspace. Insert a BoolValue into that Brick. Name that BoolValue "OnOff". Just as before, you have a Hopperbin object in the StarterPack, and a script in the HopperBin. Insert the following into that script:

brickName = "Toggle"
valueName = "On/Off"

function onButton1Down(mouse)
	local targ = mouse.Target
	if targ~=nil then
		if targ.Name == brickName then
			if targ:findFirstChild(valueName)~=nil then
				if targ[valueName].Value == true then
					targ[valueName].Value = false
print("one")
				else
					targ[valueName].Value = true
print("two")
				end
			end
		end
	end
end

function onSelected(mouse)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end

script.Parent.Selected:connect(onSelected)

Upon clicking the mouse on the brick in Solo Mode, you can see the Output toggle between "one" and "two". You can easily manipulate this script to work for anything.

Mouse Without HopperBin

Setting up the HopperBin

You need a HopperBin object. This is what the player selects to activate the whole system.

You also need two scripts. Here is how they should look in the Explorer:

[-] HopperBin
	ConnectionScript
	ControlScript

The script named ConnectionScript is the one that sets it all up, and links the mouse to some values. The HopperBin gets removed, the two scripts moved, but the mouse is still active until you select a different HopperBin.

ConnectionScript

Here is what goes in "ConnectionScript":

function onSelected(mouse)
	local m = Instance.new("CFrameValue")
	m.Parent = script.Parent.Parent.Parent
	m.Name = "MouseCFrame"
	local b = Instance.new("BoolValue")
	b.Parent = script.Parent.Parent.Parent
	b.Name = "Button1Down"
	local k = Instance.new("StringValue")
	k.Parent = script.Parent.Parent.Parent
	k.Name = "KeyPressed"
	local t = Instance.new("ObjectValue")
	t.Parent = script.Parent.Parent.Parent
	t.Name = "MouseTarget"
	mouse.Move:connect(function() m.Value = mouse.Hit end)
	mouse.Button1Down:connect(function() b.Value = true end)
	mouse.Button1Up:connect(function() b.Value = false end)
	mouse.KeyDown:connect(function(key) k.Value = key k.Value = "" end)
	mouse.Changed:connect(function(property)
		if (property == "Target") then
			t.Value = mouse.Target
		end
	end)
	local hb = script.Parent
	script.Parent = hb.Parent
	hb.ControlScript.Parent = script.Parent
	hb:Destroy()
end

script.Parent.Selected:connect(onSelected)

ControlScript

Here is the ControlScript:


while true do
	if script.Parent.Name == "Backpack" then  -- this is to pause the script until it gets moved, no bugging up.
		break -- this stops the while loop. 'breaking' it
	end
	wait()
end

function newText(text)
	local nm = Instance.new("Hint")
	nm.Parent = p
	nm.Text = text
end

function onKeyDown(key)
	if script.Parent.Parent.Character==nil then return end
	local key = string.lower(key)
end

function onButton1Down()
	if script.Parent.Parent.Character==nil then return end		
end

function onButton1Up()
	if script.Parent.Parent.Character==nil then return end
end

function onMove()
	if script.Parent.Parent.Character==nil then return end
end

script.Parent.Parent.KeyPressed.Changed:connect(function() onKeyDown(script.Parent.Parent.KeyPressed.Value) end)
script.Parent.Parent.MouseCFrame.Changed:connect(function() onMove() end)
script.Parent.Parent.Button1Down.Changed:connect(function() 
	if script.Parent.Parent.Button1Down.Value == true then 
		onButton1Down() 
	else
		onButton1Up()
	end
end)

How To Make onKeyDown Scripts with HopperBins

As above, you will need a HopperBin in the StarterPack, and an empty Script in the HopperBin.

The Basic Script

You will need to add different parts to the basic script to make it work. It must have :

  • An "if key == "(key)" then" line.
  • Sometimes an or line. There can be more than one.

Here is the unedited, basic script:

function onKeyDown(key) 
key:lower() 
--*********************--
--*********************--
--*********************--
--*********************--
--*********************--
end 


function onSelected(mouse) 
mouse.KeyDown:connect(onKeyDown) 
end 

script.Parent.Selected:connect(onSelected)

Put the if line where the commented stars are. The lines where the stars are, are also where the stuff happens. For example, if you press "f", you die. So the lines would be

if key == "f" then 
script.Parent.Parent.Parent.Character.Humanoid.Health = 0
end
  • This finds the script's parent [your HopperBin], its parent [your backpack] its parent [your player] then your Character [you in the workspace] then your Humanoid, and finally its health.
  • You can also change onKeyDown(key) to onButton1Down(mouse). Make sure you change the connection line as well.

A line with or in it would look like this :

if key == "b" or "h" or "k" then --Etc.
--The things you want to happen go here.
end

An example of a script that kills the player via random explosion would be :

e=Instance.new("Explosion")
function onKeyDown(key) 
key:lower() 
if key == "f" then 
e.Position = script.Parent.Parent.Parent.Character.Head.Position
e.Parent = game.Workspace
e.BlastRadius = 10
e.BlastPressure = 100000
end

end 


function onSelected(mouse) 
mouse.KeyDown:connect(onKeyDown) 
end 

script.Parent.Selected:connect(onSelected)

To make sure that the script works, it is recommended to use Output. It will tell you exactly what you messed up on, and even what line.