Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Destroy All Barbarian Models?

I am trying to make a reset button then if i've spawned like 15 zombie then if i click on the reset button them all just destroys but cant get it to work it just destroys one of them every time i click on the reset button heres the code

local Player = game.Players.LocalPlayer

Cash = Player.leaderstats.Cash
Units = Player.leaderstats.Units

function onClick()
	if game.Workspace.Barbarian then
		game.Workspace.Barbarian:Destroy()
	end
	Cash.Value = 100
	Units.Value = 0
end

script.Parent.MouseButton1Click:connect(onClick)

1 answer

✓ Accepted answer

The problem is that your current code only destroys the first Barbarian it finds every time the button is clicked. To delete all of them, you need to loop through all of the workspace's children, decide if it is a Barbarian, and if so, delete it.

local Player = game.Players.LocalPlayer

Cash = Player.leaderstats.Cash
Units = Player.leaderstats.Units

function onClick()
    for i, v in ipairs(game.Workspace:GetChildren()) do
        if v.Name == "Barbarian" then
            v:Destroy()
        end
    end
    Cash.Value = 100
    Units.Value = 0
end

script.Parent.MouseButton1Click:connect(onClick)

This discussion is preserved as it was. New answers, comments, and votes are closed.