A tool has three key events you can connect to: Equipped, Unequipped and Activated.
||Fired when a tool is equipped by the player, for example when a tool is selected in the hotbar.|
||Fired when a tool is unequipped by the player, for example when a tool is deselected in the hotbar.|
||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
Soundobjects 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.Parent local function toolEquipped() tool.Handle.Equip:Play() end local function toolActivated() tool.Handle.Activate:Play() end tool.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.
Previous Page Getting Started