I want to fix the Tool:Destroy() problem. It doesn’t destroy, equipped or unequipped.
The tool’s CanBeDropped is turned off, tool is Enabled, and has no more than one “Handle”. The “Handle” is a BasePart.
The function:
print("Cleaning up hiders")
for _, hider in ipairs(modules.game_stuff.hiders) do
if hider then
local backpack: Backpack = hider:WaitForChild("Backpack")
local hider_char: Model = hider.Character or hider.CharacterAdded:Wait()
-- destroy the tools
for _, tool in ipairs(backpack:GetChildren()) do
if not tool then
-- the equipped tool
for _, tool in pairs(hider_char:GetChildren()) do
if tool and tool:IsA("Tool") then
print("Destroying equipped:", tool.Name)
tool:Destroy()
end
end
else
-- the unequipped tools
if tool and tool:IsA("Tool") then
print("Destroying unequipped:", tool.Name)
tool:Destroy()
end
end
end
end
end
I fail to see the point in this if not tool then
An object will never be falsey, thus there’s no point to this (not to mention it will cause the hider_char check to not run ever). Rather to make sure that the tools are destroyed, you could simply skip that check and go to destroying the tools.
local function destroyHiderTools(par)
if par == nil then return end
for _, tool in ipairs(par:children()) do
if tool.ClassName == "Tool" then
tool:Destroy()
end
end
end
...
for _, hider in ipairs(modules.game_stuff.hiders) do
print("Removing tools for <", hider.Name, ">")
local backpack = hider:FindFirstChild("Backpack")
local hider_char = hider.Character
destroyHiderTools(backpack)
destroyHiderTools(hider_char)
end
Ah, that explains that. Remember, lua is case-sensitive. There’s no method for objects called Children. There is, however, one called GetChildren. children is the deprecated form of GetChildren that I still use.
local function destroy_hider_tools(par)
if par == nil then return end
for _, tool in ipairs(par:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
end
if next(modules.game_stuff.hiders) ~= nil then
print("Cleaning up hiders", modules.game_stuff.hiders)
for _, hider in ipairs(modules.game_stuff.hiders) do
print("Removing tools for <", hider.Name, ">")
local backpack = hider:FindFirstChild("Backpack")
local hider_char = hider.Character or hider.CharacterAdded:Wait()
if backpack ~= nil then
destroy_hider_tools(backpack)
else
warn("No backpack!")
return
end
if hider_char ~= nil then
destroy_hider_tools(hider_char)
else
warn("No character!")
return
end
end
-- after the hiders' cleanup, say cheese
table.clear(modules.game_stuff.hiders)
else
warn("No hiders!")
return
end
There’s no point to including if next(modules.game_stuff.hiders) ~= nil then
A generic for will not run the code if the table’s empty.
if backpack ~= nil then
destroy_hider_tools(backpack)
else
warn("No backpack!")
return
end
I don’t recommend having this check present. If the hider doesn’t have a backpack nor character, it’ll exit out the code and won’t check the rest of the hiders. The checks there can be removed entirely, as there’s already a safe-check for that in the destroy_hider_tools code.
local backpack = hider:FindFirstChild("Backpack")
local hider_char = hider.Character
destroyHiderTools(backpack)
destroyHiderTools(hider_char)