Cookbook (Chapter 5)

From Legacy Roblox Wiki
Jump to navigationJump to search

Introduction

Have you ever been in a game and seen that they had a rocket launcher? You have have also seen something where you could reset yourself? What is this madness and how can we create our own? We first must differentiate a Tool and a HopperBin. A Tool provides one with the ability to get access to the Mouse object and provides an easy way to interface with a physical weapon or tool. A HopperBin is a tool without the weapon.

Just like GUI, Tools and HopperBins are placed within a special directory and distributed to all the players. This directory is called the StarterPack. When a Player is added, a copy of each member in the StarterPack is copied into a player’s Backpack, similar to the PlayerGui directory.

Tools have two parts, the actual Tool object itself, and the “Handle”. The handle is a part inside of the tool that acts as the weapon. When one selects the Tool using the keyboard or pressing its GUI, the Handle is attached to the character’s right arm. Make sure that handles are named “Handle”, or else the Tool won’t recognize it and won’t equip the character with it.

Creating a Reset

Problem

You want to create a HopperBin that resets your character.

Solution

Use the Selected, and Button1Down events within a LocalScript.

script.Parent.Selected:connect(function(m)
	m.Button1Down:connect(function()
		if game.Players.LocalPlayer.Character then
			game.Players.LocalPlayer.Character:BreakJoints()
		end
	end)
end)

Discussion

Since Tools and HopperBins are client side objects (meaning they exist on a per-client basis, see Recipe 4.6 for details) we can use a LocalScript in order to access local shortcuts (actually due to a ROBLOX update, in order to work with HopperBin and Tool events, you must use a LocalScript).

We first connect the Selected event to “script.Parent”, I’m assuming this LocalScript is a direct child of the HopperBin. Then we connect Button1Down to “m” which is an argument passed to the callback of the Selected event. The “m” is a MouseObject which has access to quite a few interesting events, one of which is Button1Down which fires when the left mouse button is in the down state. From inside the callback we check to see if the LocalPlayer has a character, if so then we call the method BreakJoints on it.

Calling BreakJoints on the character model will kill the character by breaking up all the joints connecting the character together.

Kill Tool

Problem

You want to create a HopperBin that kills people when you click on them.

Solution

Use the Selected, and Button1Down events, with the Hit property of Mouse.

script.Parent.Selected:connect(function(m)
	m.Button1Down:connect(function()
		local p = game.Players:FindFirstChild(m.Target.Parent.Name)
		if p and p.Character then
			p.Character:BreakJoints()
		end
	end)
end)

Discussion

When the HopperBin is selected, and the left mouse button is on the down state, we create a variable called “p” which is set to “game.Players:FindFirstChild(m.Target.Parent.Name)”. The Target property of the Mouse is what the user last clicked on. If the local player clicked on the “Head” of another player, then m.Target would be the Head. In order to get the potential character name, we have to get the name of the parent which would be the character and then get its name. We look inside of the Players directory for that name.

If we find that player and that player has a character, then we break the joints of the character killing it.

Key Controls

Problem

You want to listen for key presses with a HopperBin.

Solution

Use the KeyUp, and KeyDown events.

script.Parent.Selected:connect(function(m)
	m.KeyDown:connect(function(k)
		print(k)
	end)
	m.KeyUp:connect(function(k)
		print(k)
	end)
end)

Discussion

In this HopperBin we connect two functions, KeyUp and KeyDown. When the user presses a key down while having the HopperBin selected, it fires the event giving the callback function the key in which was pressed.

Creating a Sword

Problem

You want to create a sword.

Solution

Use the Equipped event with a Tool.

script.Parent.Handle.Touched:connect(function(hit)
	local pl = game.Players:FindFirstChild(hit.Parent.Name)
	if pl and pl ~= game.Players.LocalPlayer then
		pl.Character:BreakJoints()
	end
end)
 
script.Parent.Equipped:connect(function(m)
	m.Button1Down:connect(function()
		local n = script.Parent.Parent.Character.Torso['Right Shoulder']
		repeat wait() n.MaxVelocity = .4 n.DesiredAngle = math.pi until math.ceil(n.CurrentAngle) == math.ceil(math.pi)
		repeat wait() n.MaxVelocity = .4 n.DesiredAngle = -.5 until math.ceil(n.CurrentAngle) == math.ceil(-.5)
	end)
end)

Discussion

First we use the Handle within the Tool (which is going to be the physical sword) and make it kill when someone (besides you) touches it. Then when the Tool is Equipped (equal to Selected in a HopperBin), and when the user clicks, we do a sword swinging animation.

Creating a Kick Tool

Problem

You want to create a HopperBin that when you click on a player, they are kicked from the server.

Solution

Remove their Player when you click on them.

script.Parent.Selected:connect(function(m)
	m.Button1Down:connect(function()
		local p = game.Players:FindFirstChild(m.Target.Parent.Name)
		if p then
			p:Destroy()
		end
	end)
end)

Discussion

When the HopperBin is selected, we find the Player associated with the Character which we clicked on. Then we simply remove the Player object to kick them. Do note that they may rejoin the server after they’ve been kicked.

StarterGear

Once you die, all objects in your Backpack are cleared. This means that when you die, you loose your tools. What if you wanted to create a permanent Tool or HopperBin. One that acts like gear because when you die, your gear is not revoked.

Instead of putting your HopperBins and Tools inside of the StarterPack or Backpack, you can put them into a directory called StarterGear. This is located within the Player directory. You will be able to spawn with all the objects placed into that directory. Also note that this is where all your real gear is put when you join a game.

Continue on to Chapter 6, Service Level