I've been coding a game for the past couple days and I ran into this problem that I have yet to figure out. So a table gets created that gets the children of the players Character. So now at this point we have our table created and we sort through it. Then the problem comes in when I make a string of Names that belong in the players Character that I do not want to delete but instead it deletes everything inside the players Character. I know what the problem is just I do not know how to go about fixing it since I'm just now getting started in Tables. I hope I worded this correctly but any help would greatly be appreciated! I think I need to run through the entire string before I run the :Destroy function but I don't know how I would do that.
local object = script.Parent local partsToKeep = {"Head", "HumanoidRootPart"} ----Parts I want to keep local function infectPlayer(hit) if hit.Parent:FindFirstChild("Humanoid")then if hit.Parent.Infected.Value == false then hit.Parent.Infected.Value = true local playersTable = hit.Parent:GetChildren() for i, v in pairs(playersTable) do print(i .. " is = to " .. v.Name) for _, p in pairs(partsToKeep) do if v.Name ~= p.Name then ----Possible broken code v:Destroy() end end end end end end object.Touched:Connect(infectPlayer)
I've came up with some new code that correctly works. If you have a better way to script it let me know, I'm always willing to learn. Fixed Code is below
local object = script.Parent local partsToKeep = {"Head", "HumanoidRootPart"} local killPart = nil local function infectPlayer(hit) if hit.Parent:FindFirstChild("Humanoid")then if hit.Parent.Infected.Value == false then hit.Parent.Infected.Value = true local playersTable = hit.Parent:GetChildren() for i, v in pairs(playersTable) do ---print(i .. " is = to " .. v.Name) for _, p in pairs(partsToKeep) do if v.Name == p then killPart = 1 print(p) end end if killPart == 1 then killPart = 0 else v:Destroy() end end end end end object.Touched:Connect(infectPlayer)