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

3 comments
hey can you please explain where the soldiers are placed inside the workspace and what are their names then i'll be able to help you!
also all you want is to delete them after the fight (after they all died), is that right?
there is a model in the workspace, in the model are unions, in the unions are the soldiers and some values. Their names are: player.Name+'s soldier+the amount of soldiers on that part. And yes, I only want to delete them, but not all of them, only the amound of fromsoldiers.