How can i not unequip a tool but you can only switch tools? I can't really wrap my head around it. I didn't do the code when you switch tools. I only tested it for one tool but when i unequip the tool it lags then freezes. This is the error: ? Something unexpectedly tried to set the parent of FastCastGun to ProGamer(Character) while trying to set the parent of FastCastGun. Current parent is Backpack. (x2). This error keep repeating simultaneously with the lag. How can I fix it and how to make the script when you can only switch tools? This applies in many fps games.
Heres my code:
local animateequip = script.Parent.animationequip
local anim = script.Parent.Animation
local equipped = false
animateequip.OnServerEvent:Connect(function(player)
equipped = true
if equipped == true then
local hum = player.Character.Humanoid
local equip = hum:LoadAnimation(anim)
equip:Play() -- is it the animation?
end
end)
local function characteradded(character)
local hum = character.Humanoid
print(character)
hum:EquipTool(script.Parent)
script.Parent.Unequipped:Connect(function()-- this one maybe
equipped = false
if equipped == false then
equipped = true
hum:EquipTool(script.Parent)
end
end)
end
local function playeradded(player)
player.CharacterAdded:Connect(characteradded)
local charact = player.Character
if charact then
characteradded(charact)
end
end
game.Players.PlayerAdded:Connect(playeradded)
for i,v in pairs(game.Players:GetPlayers()) do
playeradded(v)
end

10 comments
Thank you so much for explaining, I really appreciate for your effort answering my question.
After i equip then unequip a couple of times it freezes roblox studio.
@RichDiggerW189 is there an error?
try adding some sort of cooldown or debounce
i added a cooldown but still not working here's my script
script.Parent.Unequipped:Connect(function() local tool = backpack.ChildAdded:Wait() local reequip = false if script.Parent == tool and reequip == false then reequip = true RunService.Heartbeat:Wait() char.Humanoid:EquipTool(script.Parent) print("Re-equippingTool") wait(0.5) reequip = false else print("SwitchingTool") end end)
@Marked Tomato yep there is still an error
sorry if I am very late and the cooldown script is a bit messy, I apologize
@RichDiggerW189 make it as a question
I tried to avoid using wait() as much as possible because I've seen a post on the developer forum talking about that wait() waits longer than you think and can cause performance issues