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

How do i make the player unequip a tool through a script?

Asked by
Echtic 128
4 years ago

I just need the player to unequip a tool once a certain condition is met, i have everything done just need the function itself, couldn't find anything regarding that on the developer wiki.

2 answers

Log in to vote
0
Answered by 4 years ago

you can use the humanoid:UnequipTools() function, demonstrated in the code below:

local Players = game:GetService("Players") -- gets the player service
local ContextActionService = game:GetService("ContextActionService") -- gets the cas service

local localPlayer = Players.LocalPlayer -- gets the local player

ContextActionService:BindAction("unequipTools",  -- binds action to a function
    function(_, userInputState)
        if userInputState == Enum.UserInputState.Begin then 
            if localPlayer.Character then -- checks if the player has a character
                local humanoid = localPlayer.Character:FindFirstChildOfClass("Humanoid") -- checks if the player has a humanoid
                if humanoid then  -- checks if the player has a humanoid
                    humanoid:UnequipTools() -- unequips any equipped tool
                end
            end
        end
    end,
    false, 
    Enum.KeyCode.U -- sets the keybind, in this example U, when a player presses the U key, any equipped tool will be unequipped.
)
  • avenze
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
plr = game.Players.LocalPlayer
backpack = plr.Backpack -- player's backpack
tooleq = plr.Character:FindFirstChild("Tool")
tooluneq = backpack:FindFirstChild("Tool")

if tooluneq then return end
if tooleq then
    tooleq.Parent = backpack
end
--im bad at english btw

just change da parent of da tool, when it's equip it will move to character and when it's unequip it will move to player's backpack

Answer this question