PcoWSkbVqDnWTu_dm2ix
Creating Player Tools
Part 2 - Using Scripts with Tools
Creating Player Tools
Part 2 - 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.

  1. Insert two Sound objects into the Handle.
  2. Rename one sound Equip and set its SoundId property to rbxassetid://282906960.
  3. 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.

  1. Insert a LocalScript into the tool and name it ToolController.
  2. 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)
  3. 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