How to update values from inside a loop

Hello developers, I am currently working on a while loop which increases a value every 10 seconds, however if the value changes I can’t get the new value from inside the loop:

local Players = game:GetService("Players")
local Player = Players:GetPlayerByUserId(tonumber(script.Name))

local seconds = 10

local label = Player:WaitForChild("PlayerGui"):WaitForChild("StageGui"):WaitForChild("Time"):WaitForChild("TextLabel")
local skips = Player:WaitForChild("Skips")

local newThread = coroutine.wrap(function()
	while wait(1) do
		if seconds == 0 then
			seconds = 10
            --Value stays the same even though it is changed.
			print(Player:WaitForChild("Skips").Value)
			Player:FindFirstChild("Skips").Value += 1
		end

		seconds -= 1
	end
end)


newThread()

How can I get the right value from inside the loop? Thanks.

Are you trying to get the seconds value or the seconds value?

Attempt to use this script

local Players = game:GetService("Players")
local Player = Players:GetPlayerByUserId(tonumber(script.Name))

local seconds = 10

local label = Player:WaitForChild("PlayerGui"):WaitForChild("StageGui"):WaitForChild("Time"):WaitForChild("TextLabel")
local skips = Player:WaitForChild("Skips")

local newThread = coroutine.wrap(function()
	while wait(1) do
		if seconds == 0 then
			seconds = 10
			print(skips.Value) -- Use the 'skips' variable you defined
			skips.Value = skips.Value + 1 -- Increment 'Value' property
		end

		seconds = seconds -= 1 
	end
end)

newThread()

“Corrected the decrement syntax”?? seconds -= 1 should work just fine.

Sorry for overlooking that lol just noticed I messed that up.

If you are setting the value of a ValueObject Instance, it shouldn’t matter what context (inside a loop or not) you are setting it in, the value should be consistent from wherever you reference it. If that is not the case, you may be reading a different value than the one you are writing, perhaps because you are accidently duplicating the value object somewhere. You should click Play to test in Studio and open the player object in the explorer to confirm that there is only one copy of the value object and that it is being set by your script, and you should double-check your other script to make sure you are accessing the same value object. Additionally, client changes to a value object will not replicate to a server script. If you are trying to write a value object from the client via a local script and read from the server via a server script, this won’t work. To replicate values from the client to the server, consider using a RemoteEvent or RemoteFunction instead.

The issue is: Skip value gets changed from a different script and the value gets set to 0, however instead of increasing +1 after 10 seconds it takes the old value (which should be 1 if didn’t get set to 0) and the value gets set to 2 instead of 1.

I already tested it, the case isn’t that. You can look up to the post I wrote before this.

Skip value which is under the comment.

1 Like