Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i force Unequip a tool?

I am working on a FPS game and i am trying to make it so that the current tool gtets unequipped when someone changes their gun.

This is what i have so far:

wait(1)
local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local startergui = game:GetService('StarterGui')
startergui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
p.CameraMode = Enum.CameraMode.LockFirstPerson

local gun = game.ServerStorage.Pistol:Clone()
gun.Parent = p.Backpack
c.Humanoid:EquipTool(gun)

m.KeyDown:Connect(function(key)
if key == "1" and p.Data.Current.Value ~= "Primary" then
p.Data.Current.Value = "Primary" 
c:FindFirstChild(p.Data.Primary.Value):Remove()
p.PlayerGui.mainGUI:Remove()
local gun = game.ServerStorage:FindFirstChild(p.Data.Primary.Value):Clone()
gun.Parent = p.Backpack
c.Humanoid:EquipTool(gun)
end
end)
m.KeyDown:Connect(function(key)
if key == "2" and p.Data.Current.Value ~= "Secondary" then
p.Data.Current.Value = "Secondary" 
c:FindFirstChild(p.Data.Secondary.Value):Remove()
p.PlayerGui.mainGUI:Remove()
local gun = game.ServerStorage:FindFirstChild(p.Data.Secondary.Value):Clone()
gun.Parent = p.Backpack
c.Humanoid:EquipTool(gun)
end
end)

This works and all, but i would prefer a way to forcefully unequip a tool instead of removing the tool itself.

Thanks!

1 comment

0

You have use EquipTool and there is the reverse operation http://wiki.roblox.com/index.php?title=API:Class/Humanoid/UnequipTools

1 answer

✓ Accepted answer

its possible, just move the tool to the player's "Backpack"

when equipped:

tool appears in the players model (game.Workspace.PLAYERNAME)

when NOT equipped:

the tool is stored in a folder located in the player's "Player" instance called "Backpack" (game.Players.PLAYERNAME.Backpack)

by the way, its a good habit to indent your code

3 comments

0

Yea, i understood the part where the tool goes into the character, but i didnt think about just moving it back to backpack. Also, i honestly dislike indenting.

0

You can't, when trying to change the parent of the tool this happens: " Something unexpectedly tried to set the parent of Tool to Backpack while trying to set the parent of Tool. Current parent is ____"

0

sorry for taking 4 months to respond, but the reason it gives you such an error, is because 2 scripts tried to set the parent at the exact same time, which makes absolutely no sense, try adding a wait()

This discussion is preserved as it was. New answers, comments, and votes are closed.