I want to make a system where if an enemy dies, the defeated and points value will increase.
But, it seems to run multiple times. I tried changing the position of my in pairs loop a bit, but it still wouldn’t work. Do I need to make a new script?
while true do
wait()
for i, stale in pairs (workspace.Stale:GetChildren()) do -- Healthbar
local humanoid = stale.Humanoid
local maxhealth = humanoid.MaxHealth
local billboardGUI = humanoid.Parent:WaitForChild("BillboardGui")
local frame = billboardGUI:WaitForChild("Frame")
local textbox = frame:WaitForChild("TextBox")
local health = humanoid.Health
textbox.Text = tostring(health).." / "..tostring(maxhealth)
if health <= 0 then -- If it died
for i,parts in pairs (stale:GetChildren()) do --Remove all parts
if parts:IsA("BasePart") and parts.Name ~= "HumanoidRootPart"then
parts:Destroy()
frame.Visible = false
end
end
for i, plr in pairs (game.Players:GetPlayers()) do --Player's leaderstats
points = plr.leaderstats.Points
enemiesdefeated = plr.leaderstats.Defeated
end
enemiesdefeated.Value = enemiesdefeated.Value + 1 --Defeated stats +1, points + 500
points.Value = points.Value + 500
end
end
end
Where should I put this for it to only run once?
enemiesdefeated.Value = enemiesdefeated.Value + 1 --Defeated stats +1, points + 500
points.Value = points.Value + 500