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

I making a script for my game that resets the leaderboard status but its not working?

Here is the script so far -

while true do
	wait(300)
	StatName = "Kills"
	Amount = 0
	wait(.1)
	Player = game.Players:GetPlayerFromCharacter(workspace[PlayerName])
	StatLeaderboard = Player.leaderstats[StatName]
        StatName = "Deaths"
	Amount = 0
	wait(.1)
	Player = game.Players:GetPlayerFromCharacter(workspace[PlayerName])
	StatLeaderboard = Player.leaderstats[StatName]

end

I want to reset the leader stats every 5 min for all players and the stats are Kills and Deaths

The script isn't working though and I dont know why, can you help me

Also if the script does look terrible, I only started a year ago

1 comment

0

I dont know how to code this but you need a function to get all the children in players. Change there intvalue i think

1 answer

✓ Accepted answer

I rewrote this to give you a better idea of how it should look. You can add some more if checks but if the rest of your code is working correctly you shouldn't have any errors.

Your code has several issues, but most importantly this 'Amount = 0' doesn't do anything. You need to create the values on entry, not at some interval.

--a simple function to iterate all players turning the specified objects value to 0, this is more flexible since it allows for any amount, any stat(inside leaderstats), and any group of players
function resetStat(players,stat,amount)
	for i,v in pairs(players) do
		v.leaderstats[stat].Value = amount
	end
end

--this adds the leaderstats folder and corresponding values when player joins the game
game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Parent = Player.leaderstats
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = Player.leaderstats
end)

--much cleaner, right? 
while wait(300) do
	local players = game:GetService('Players'):GetChildren()
	resetStat(players, 'Kills', 0)
	resetStat(players, 'Deaths', 0)
end

8 comments

1

Line 4 will fail

0

Good eye, should've ran it through studio first.

0

Happens to the best of us ;)

0

It might also be better to assert the stat, to accommodate for existence issues!

0

thanks 4 the help

0

wait I just tried the script and it didn't work, should of read Ziffixture comment first. I don't understand what is wrong with line 4?

0

Whats the error? The problem he mentioned is fixed.

0

sorry i tried it before it was fixed

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