I’m working on a Raycasting gun for my game, and im using FindPartOnRayWithIgnoreList to add a blacklist to accessories, however, when I attempt to remove the hats from the array when the character dies, none of the hats end up getting removed
local function findInTable(tbl, element)
for _, v in ipairs(tbl) do
if (rawequal(v, element)) then
return true
end
end
return false
end
local function removeValuesFromArray(array, value)
for i = #array, 1, -1 do
if array[i] == value then
table.remove(array, i)
end
end
end
local function gethats(p)
for na,ni in pairs(p:GetChildren()) do
if (ni:IsA("Accoutrement") or ni:IsA("Tool") or ni:IsA("Accessory")) and ni:FindFirstChild('Handle') then
table.insert(hatlist, ni.Handle)
end
gethats(ni)
end
end
local function removehats(p)
for na,ni in pairs(p:GetChildren()) do
if (ni:IsA("Accoutrement") or ni:IsA("Tool") or ni:IsA("Accessory")) and ni:FindFirstChild('Handle') then
if findInTable(hatlist, ni.Handle) then
removeValuesFromArray(hatlist, ni.Handle)
end
end
removehats(ni)
end
end
local function PlayerAdded(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChild("Humanoid")
gethats(player)
end)
player.CharacterRemoving:Connect(function(character)
removehats(player)
end)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
gethats(workspace)
for i,v in next,game.Players:GetPlayers() do
PlayerAdded(v)
end
The script is a ServerScript parented to a tool, If anyone could possible help me figure out the issue, that would help me a lot