Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do Equip and Unequip Tools in a keybind? [Solved]

Asked by 4 years ago
Edited by Ziffixture 4 years ago

So Basically, I already know how I can equip a "Tool" using a key bind Here is my Code, For a FlashLight

local player = game.Players.LocalPlayer
local character = player.Character
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
        if inputObject.KeyCode == Enum.KeyCode.R then
            print("R was pressed")

        if player and player.Character then
    local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        local tool = workspace:FindFirstChild("Flashlight")
        if tool then
            humanoid:EquipTool(tool)    
        end
    end
end
        end
        end)

So This script makes you equip "Tool" when you click R but I don't know where to put the "Humanoid: UnequipTool(tool)" in this part

So Basically This is my general question: How do I make my script when I press "R" it equips the tool but when I press "R" again it unequips the tool

sorry I was confused about this

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Okay i Found out that 2 local script is needed about this, one is Equip and one is Unequip

Put those local scripts at the StarterGui

You need 2 Tools, One is in the StarterPack, and the other One is in the Replicated Storage for cloning

For the Equip Local Script


local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() Mouse.KeyDown:connect(function(Key) Key = Key:lower() if Key == 'r' then game.Workspace:FindFirstChild(Player.Name).Humanoid:EquipTool(Player.Backpack."Your Tool") script.Parent.Unequip.Disabled = false script.Disabled = true end end)

For the Unequip Local Script

local Player = game.Players.LocalPlayer
    local Mouse = Player:GetMouse()

    Mouse.KeyDown:connect(function(Key)
            Key = Key:lower()
            if Key == 'r' then
        game.Workspace:FindFirstChild(Player.Name)."Your Tool":remove()
        game.ReplicatedStorage."Your Tool":clone()
        game.ReplicatedStorage."Your Tool":clone().Parent = game.Players.LocalPlayer.Backpack
        script.Parent.Equip.Disabled = false
        script.Disabled = true
            end
    end)


Ad

Answer this question