I want to make a game where soldiers can fight each other, but I have trouble deleting the soldiers after the fight. Can someone help me with that? This is the code I have now:
local fromsoldiers = {} local tosoldiers = {} for i,v in pairs(attackedfrom:GetChildren())do if v.ClassName == "Model" then fromsoldiers[#fromsoldiers+1] = v end end for i,v in pairs(attackedto:GetChildren())do if v.ClassName == "Model" then tosoldiers[#tosoldiers+1] = v end end --compare if #tosoldiers-#fromsoldiers>=1 then print("defence won") for i=1,#fromsoldiers do print(tosoldiers[#tosoldiers-i]) --everything is fine for i,v in pairs(attackedto:GetChildren())do if v.ClassName == "Model" then if v.Name == tosoldiers[#tosoldiers-i] then v:Destroy() --doesnt work end end end end end
edit: I changed part of my code
if p.Name == tosoldiers[#tosoldiers-i].Name then print(s,p.Name.." is the same as "..tosoldiers[#tosoldiers-i].Name) else print(s,p.Name.." and "..tosoldiers[#tosoldiers-i].Name.." are not the same") end
but now it gives a really weird output:
defence won Player2's soldier0 and Player2's soldier1 are not the same Player2's soldier1 is the same as Player2's soldier1 Player2's soldier2 and Player2's soldier1 are not the same Player2's soldier0 is the same as Player2's soldier0 Player2's soldier1 and Player2's soldier0 are not the same Player2's soldier2 and Player2's soldier0 are not the same
edit: i understand it now make it get all the children in the folder and set there parent to a folder after the battle then delete the folder and recreate it afterwards