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

How do i force Unequip a tool?

Asked by
Nikkulaos 229 Moderation Voter
6 years ago

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!

0
You have use EquipTool and there is the reverse operation http://wiki.roblox.com/index.php?title=API:Class/Humanoid/UnequipTools User#5423 17 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

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

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. Nikkulaos 229 — 6y
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 ____" FadedDragoner 15 — 4y
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() fanofpixels 718 — 4y
Ad

Answer this question