How can i reset every player's leaderstats down to 0?

hello all! i’ve been working on a soccer game with a friend, me taking the scripting role, however, i’m at a roadblock, i need to handle the resetting of the “Goals” leaderstat after a match ends (i’m using it as a temporary stat to measure how many goals each player has hit), my issue is with how i’d do it, right now, i have this function that’s meant to reset both the team stats (i have numbervalues inside each team), as well as the leaderstats, however, it only resets the team stats, without resetting the leaderboard stats, here is my function

local teams = game.Teams:GetChildren()
local players = game.Players:GetChildren()
 -- two variables defined earlier in the script
local function resetStats()
	for i, player in ipairs(players) do
		player.leaderstats.Goals.Value = 0
		print("cleared leaderstats for player "..player.Name)
	end
	
	for i, team in ipairs(teams) do
		team.Points.Value = 0
	end
end

the one resetting the teams score works just fine, and does what it should do, however, the one resetting leaderstats does absolutely nothing, which sucks, since i’m using this to reset everyone’s stats to prepare a new match from scratch.

(for context, i’m a bit new to messing with leaderboards, so i might be a little dumb with this, sorry if anyone has to suffer from this)

does anyone know how to help me with this situation? i’m not using datastores, or any form of data saving, if you couldn’t tell. thanks all!

Try using pairs instead of ipairs?

1 Like

doesn’t change anything at all, unfortunately

Oh, you should remove the players variable. If new players join, they won’t be apart of that list. So instead, use game.Players:GetPlayers() directly in pairs.

1 Like

Im not sure if this would help since they arent said it didnt work

Im pretty sure ipairs would work as thr code returns an array of players and ipairs works with arrays

the problem is when it gives this value at the beginning of the game (or at any time), it takes the instances of the current players
Remove :GetChildren() and use inside pairs()

(i used translate my english bad sorry)

1 Like

thank you so much! this fixed it, i’ll take this knowledge and use it very wisely

I already said that, but it’s fine. Glad you found the solution!

1 Like

Ah, sorry, i wasn’t exactly able to comprehend how to apply it into the for loop, so the one i marked as solution was more comprehensive

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.