Tools are a simple way to manage items that the player can hold in their hand and use in-game. They can range from weapons such as swords to food items.
In this tutorial, you'll learn how to create a tool in the shape of a laser blaster that will play sound effects when equipped or activated.
Creating the Tool
The Tool object is the basis of any tool in Roblox, so you'll need to create one. It's easier to change how tools look by adding objects such as Parts and MeshParts to the tool in the workspace where they are visible.
Insert a Tool into the workspace and name it Blaster.
Insert a MeshPart into the tool.
Set the MeshId property to rbxassetid://92656610.
Set the TextureId property to rbxassetid://92658105.
The tool needs a part named Handle for the player to hold. Change the name of the MeshPart to Handle.
Storing the Tool
Tools can be kept in the game world as collectable tools or given to all players as starter tools.
Collectable Tool
The blaster is currently a child of Workspace so it will be collectable. A player can pick up the tool by touching it, causing it to become a child of the character model; the tool will then be equipped and placed in their hotbar.
During gameplay, unequipped tools are stored within the player's hierarchy in the Backpack and then moved to their character model when equipped. Any tool that becomes a child of a character will be automatically equipped.
Starter Tool
Storing a tool in StarterPack will place it in a player's Backpack when they join the game or respawn.
Move the Blaster to StarterPack in the Explorer.
Play the game to test the tool. Click on the hotbar at the bottom of the screen or press 1 on the keyboard to equip the tool.
Tool Properties
Position / Orientation
A tool's position and orientation can be changed using the grip properties. GripPos changes the position of the grip, whereas GripForward, GripRight and GripUp affect the rotation.
Currently the player is holding the center of the blaster instead of the grip.
Set the GripPos property of the tool to 0, -0.4, 1.1.
Click the Play button to test the tool. Notice how the tool is now being gripped in a different position.
Hotbar Icon
By default, the tool name will be displayed on the hotbar icon. It's good practice to change the icon to be an image of the tool. Set the TextureId property of the tool to rbxassetid://92628145.
Tooltip
A tooltip is a small text description that appears when the mouse hovers over a tool in the hotbar. They typically include the name of the tool and/or a brief description of its function. Change the ToolTip property to Blaster.
Using Scripts with Tools
A tool has three key events you can connect to: Equipped, Unequipped and Activated.
Event | Description |
---|---|
Equipped | Fired when a tool is equipped by the player, for example when a tool is selected in the hotbar. |
Unequipped | Fired when a tool is unequipped by the player, for example when a tool is deselected in the hotbar. |
Activated | Fired when a tool is activated by the player, for example when a player left-clicks. |
These methods only work in LocalScripts because only the player's device knows when input happens, such as a mouse button being clicked or a screen being touched.
Adding the Sounds
To see these events in action, you can play a sound when they fire. First, you'll need to create Sound objects to use for this.
Insert two Sound objects into the Handle.
Rename one sound Equip and set its SoundId property to rbxassetid://282906960.
Rename the other sound Activate and set its SoundId property to rbxassetid://130113322.
Adding the Code
The example code below plays the Equip sound when the tool is equipped and the Fire sound when activated.
Insert a LocalScript into the tool and name it ToolController.
Insert the following lines of code into the script.
local tool = script.Parentlocal function toolEquipped()tool.Handle.Equip:Play()endlocal function toolActivated()tool.Handle.Activate:Play()endtool.Equipped:Connect(toolEquipped)tool.Activated:Connect(toolActivated)Test the blaster sound effects by equipping and clicking to activate the tool.
Now you know how to create and script a basic tool, try creating other simple tools such as a flashlight or speaker.