IntValue not being reset to zero

  1. What do you want to achieve? Keep it simple and clear!

I want to reset a player’s score at the end of a round in a game I am making. This is done via the standard method of leaderstats, nothing fancy.

  1. What is the issue? Include screenshots / videos if possible!

When I try to set the value of the IntValue storing the score, it does nothing. No error, no reset score, nothing.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I realised that I was changing the IntValue, not the value of the IntValue, so I fixed that, but it still doesn’t work.

Here is the code:

local roundTime = 60
local intermission = 30

while true do
	wait(intermission) -- Intermission wait
	print("Round Start")
	local plrs = game.Players:GetPlayers()
	for i, v in ipairs(plrs) do
		local num = math.random(1, 10)
		v.Character.Head.CFrame = CFrame.new(workspace.Teleports["Teleport"..num].Position)
	end
	wait(roundTime) -- Round time
	print("Round end")
	plrs = game.Players:GetPlayers()
	for i, v in ipairs(plrs) do
		v.leaderstats.Plastic.Value = 0
		print("Plastic reset")
		v.Character.Head.CFrame = CFrame.new(workspace.LobbyTeleport.Position)
	end
end

If there is anything you need, feel free to ask, and I will supply it as soon as possible.
Thanks in advance!

1 Like

Try this instead of GetChildren, however I’m not 100% sure this will fix it.

local plrs = game.Players:GetPlayers()

Otherwise, have you tried using _,v in pairs instead of for i = 1, #plrs?

3 Likes

Hello!
As @joseph2235 suggested, try using :GetPlayers(). In case there is another object inside the players service, it’d be indexed as a player as well if you use :GetChildren().

An important thing is you need to put that same statement (i.e. create a new table with all players) right before the second iteration. Between the start and the end of the round, there’s a delay of at least 60 seconds, which means that a player could’ve left the game, so the script would error when trying to index “leaderstats” within nil.

Also I highly advise to check if a player’s character exists (e.g. when a player joins, their character is not instantly created) before trying to set its children’s properties. You want to have all possibilities covered since this is a round script and any error would result in ruining the server.

plrs[i].leaderstats.Plastic.Value = 0

Do a print right after this to check this is working. However, the issue is most likely another script overwriting this value when it shouldn’t. Make sure other code is not able to set this value after round ends.

2 Likes

I am new to using tables, would I still use plrs[i].Blah or would I use v.Blah? Also, should I use pairs or ipairs?

1 Like

You can use v.Blah if you are iterating using the for…in iteration.

Does it matter though?
30 chars

Nothing is working! I have tried everything suggested, but it still won’t reset. I don’t even get any errors! Can anyone help?

Could you share what the output says?

The only output is “Round Start”, then “Round End”, then “Plastic Reset”. The plastic value is not reset, and I get no errors.

I have updated my code, so you can give more updated help. I hope this helps!

Just to make sure, this script is a server script, correct? You cannot change values on the server such as leaderstats from the client. It will only result in the values being changed on the client, while the server values will remain unaffected.

That is incorrect, local scripts will run their code and change the value on the client, the only thing you can’t do is update them to DataStores as values changed by the client aren’t replicated to the server.

You are correct, it only updates the client and not the server. This is what I meant. I have edited my post to be more clear.

It is a server script, you are correct.

I think I found the solution, I have a localScript that registers when you pick up a piece of plastic and then adds to your score, could that have any effect on future changes to the score from the server side? Can someone tell me if I am correct?

As Quakers337 has said, you won’t see server-side changes for a value if you change that value locally. Replace the local script with a server sided script that adds points to leaderstats.

I have a solution! I made a RemoteEvent fire when the player acquired plastic, then changed the score from the server when the event fired. This then allowed the value to be reset later from the server, fixing my problems. Thankyou @Quackers337, without you telling me that you cannot change values such as leaderstats from the client, I never would have thought to change my point-adding LocalScript to a server script. The problem was that the script changing the score when you got plastic was a LocalScript.

3 Likes