So I made this game but there’s an issue where tools randomly despawn when a player tries to equip a tool. The issue is that it only happens very rarely and I have been unable to reproduce it. I’m wondering if any of you guys are able to find the cause.
This is my equipping code:
equipEvent.OnServerEvent:Connect(function(player, tool)
if tool:IsA("Tool") then
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
if tool.Parent == player.Backpack then
-- Equip the tool: first unequip all tools, then equip this one
humanoid:EquipTool(tool)
elseif tool.Parent == character then
-- Unequip the tool
humanoid:UnequipTools()
end
end
end
end
end)
equipEvent.OnServerEvent:Connect(function(player: Player, tool: Tool)
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
if tool.Parent == player.Backpack then
-- Equip the tool: first unequip all tools, then equip this one
humanoid:EquipTool(tool)
elseif tool.Parent == character then
-- Unequip the tool
humanoid:UnequipTools()
end
end
end
end)
Do you see any “Something unexpectedly tried to set the parent of [thing] to [other thing] while setting its parent to [thing]. Current parent is [the original one]” messages when this happens? If so, you will need to add a task.wait() to wait one frame before changing the parent of the tool.
On a separate note, you should probably add some validation checks to prevent exploiters from equipping tools from other players
no. when the glitch happens, there is nothing in the console which is part of the reason i have been so puzzle by it. It could be one of my other systems… in which ill lyk if im able to fix it within the next day or two. What type of validation checks might I add? Its sort of a team game so not sure why someone would want to do that in the first place.
unfortunately, that doesn’t seem to be the case as players cant find the tool on the ground… aditionally, certain tools which are not droppable such as the axe and sack are still being lost.
Do you weld the tool on the client side? It looks like the tool is falling through the world and destroying on the server side, which is why it appears as if it’s just randomly disappearing on the client side.
Have you been able to replicate the bug? What makes you think its falling through the world? I don’t weld the tools at all in scripts… i am using pre-done weld constraints for everything.
Every time I’ve experienced this issue before it was because of a weld only being created client-side. This could be a different issue though, I was just throwing out the most common cause I’ve had in my experience.
If you can, playtest then go to the server view (Test tab > Client/Server toggle) and see what happens to the tool when the player equips it.