How to script a HopperBin Tool

From Goodblox Wiki
Jump to navigationJump to search


This is a tutorial on how to script a simple Hopperbin tool in LUA. This guide is for people who already have a good knowledge of scripting, but want to learn how to make hopperbin tools.

Getting Started

First, create a new hopperbin object. This is accomplished the same way you create a new script, open the GoodBlox Studio, click the insert tab, and select object. Find Hopperbin in the list and click ok. Name the hopperbin, and make sure that it’s type is set to Script. Now, insert a new script and put it in the hopperbin. You are now ready to begin scripting the tool.

Making an Explode Tool

Start out the script by creating a function and connecting it to the hopperbin’s “Selected” event. This event returns the player’s mouse so give the function one argument named mouse. Now, create a new function and connect it to the Button1Down event of the mouse. You need to connect it like this because you will need the mouse variable later:

mouse.Button1Down:connect(function () click(mouse) end)

Your code should now look something like this:

tool = script.Parent

function click(mouse)
	
end

function selected(mouse)
	mouse.Button1Down:connect(function () click(mouse) end)
end

tool.Selected:connect(selected)

Now lets add some code to make the tool create an explosion where you click. This can be done by using the hit variable in the mouse. The “hit” varable is a CFrame value telling the position of the mouse in the game. So create an explosion and set it’s Position to mouse.hit.p

Your code should now look something like this:

tool = script.Parent

function click(mouse)
	local explosion = Instance.new("Explosion")
	explosion.Position = mouse.hit.p
	explosion.Parent = game.Workspace
end

function selected(mouse)
	mouse.Button1Down:connect(function () click(mouse) end)
end

tool.Selected:connect(selected)

Now, try it out. If you did it right, an explosion should now appear anywhere that you click. If it doesn’t work, check your code.


Now lets create a more complicated tool. We are going to create a color tool that lets you change the color of a brick that you click on.

Creating a Color Tool

First, create a new hopperbin and use the script that we created in the first part of this tutorial. Get rid of the part that creates the explosion and we are ready to get started.

Now, add a new piece of code that changes the color of the “Target” variable inside the mouse. The “Target” variable is an object value which is the brick that the mouse is pointing at. Now, test it out and see what you have created. If you need help, here is the line of code:

Mouse.Target.BrickColor = BrickColor.new(“Bright red”)

You can go ahead and make the tool work better if you want, after you are done with that continue on to the next step.

We will now make it so that you can change the color that you are changing the brick to by pressing various keys. To do this, make a new function and, in the selected function, connect it to the KeyDown event of the mouse.

The KeyDown event contains one argument. This argument is the letter of the key pressed. So give the function one argument named key.

Now we need to make it so that pressing various keys allows you to change what color the brick is changed to. I will assume you know enough to know how to do that. When you are done, your script should look something like this:

tool = script.Parent

brickColor = "Bright red"
tool.Name = "Red"

function click(mouse)
	if mouse.Target ~= nil and mouse.Target.Locked ~= true then
		mouse.Target.BrickColor = BrickColor.new(brickColor)
	end
end

function keyDown(key)
	if key == "z" then
		brickColor = "Bright red"
		tool.Name = "Red"
	elseif key == "x" then
		brickColor = "Bright green"
		tool.Name = "Green"
	elseif key == "c" then
		brickColor = "Bright blue"
		tool.Name = "Blue"
	elseif key == "v" then
		brickColor = "Bright yellow"
		tool.Name = "Yellow"
	elseif key == "b" then
		brickColor = "Bright orange"
		tool.Name = "Orange"
	elseif key == "n" then
		brickColor = "Reddish brown"
		tool.Name = "Brown"
	elseif key == "m" then
		brickColor = "Black"
		tool.Name = "Black"
elseif key == "," then
		brickColor = "White"
		tool.Name = "White"



	end
end

function selected(mouse)
	mouse.Button1Down:connect(function () click(mouse) end)
	mouse.KeyDown:connect(keyDown)
end

tool.Selected:connect(selected)