Variable (IntValue) in loop won't update

I’m trying to write a script that gives players a certain amount of money every ten seconds. The amount depends on an IntValue.

However, even if I change the IntValue, the task.spawn loop only uses the initial value from before the loop started.

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = 'leaderstats'
	folder.Parent = player
	
	local gems = Instance.new('IntValue')
	gems.Name = 'Crystals'
	gems.Value = 0
	gems.Parent = folder
	
	gps = Instance.new('IntValue')
	gps.Name = ('gps' .. player.Name)
	gps.Value = 10
	gps.Parent = workspace
	
	task.spawn(function()
		while task.wait(1) do
			gems.Value += gps.Value
--in this case it always adds 10 even if I change the int value
		end
	end)
end) 

This is the code after trying random things, no particular reason for it to be in the workspace or anything. I’ve tried getting the value through workspace.gps.Value but had the same result. The script is in ServerScriptService.

There’s a few similar posts on the forum already but the one most similar to my problem has no solutions yet (:/). Wouldn’t be surprised if there’s just a really obvious mistake I made.

In your script, I notice that gps is declared globally instead of locally. Try changing this line:

gps = Instance.new('IntValue')

To this:

local gps = Instance.new('IntValue')
1 Like

I had just noticed that they had put gps in workspace as well, which may not be the best place to have that.

@OP I don’t see why the task.spawn is needed. There doesn’t seem to be any further instructions written after it. It’s best to avoid using coroutines if they’re not necessary. It’s also not good to do while task.wait(n) do as it’s relying on the fact that Lua accepts numbers as a truthy value, which is not good practice unless that’s the intention.

local n = 1
delay(5, function() n = nil end)
while n do
	task.wait(1)
	print("cool")
end

I had gps as local, I took it out just to see if it would change anything. Forgot to fix it in the post sorry

Thanks, idk why I used task.spawn either. It still doesn’t work though so I guess that wasn’t the main problem.

Where do you change the gps variable, and could we see the code you used to change it?

I’ve just been changing it in the explorer and properties tab to see if anything works.

Can you send your current code?

Ok, I think that’s your problem. Changing the variable in the explorer tab won’t really change the value in real time. If you want to change the variable for testing, do so via a script. Alternatively, you could use this code instead:

local gpsName = "gps" .. player.Name
while task.wait(1) do
	local gpsInstance = workspace:FindFirstChild(gpsName)
	if gpsInstance then
		gems.Value += gpsInstance.Value
	end
end
2 Likes

Wait a minute, HOW are you changing gps.Value?

If you are changing in testing mode on the client, then that won’t replicate to the server, where the code is actually being ran. This might be the cause of your issue.

1 Like

Yep that was it… thank you so much that was a very dumb mistake on my part. Also is it fine to use task.wait there?

1 Like
game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = 'leaderstats'
	folder.Parent = player

	local gems = Instance.new('IntValue')
	gems.Name = 'Crystals'
	gems.Value = 0
	gems.Parent = folder

	local gps = Instance.new('IntValue')
	gps.Name = 'gps' .. player.Name
	gps.Value = 10
	gps.Parent = workspace

	task.spawn(function()
		while task.wait(1) do
			local ref = workspace:FindFirstChild('gps' .. player.Name)
			if ref then gems.Value += ref.Value
			end
		end
	end)
end)

gps is in workspace, but gems.Value += gps.Value refers to the local variable, not the actual object in workspace. You need to fetch the value dynamically each time.

Beat me by 20 min… I really need to start reading all these… :roll_eyes:

1 Like

lol thanks anyway : ) the script itself wasn’t the issue, but that’s helpful too

1 Like

I just want to add that if the same player rejoins the server, there’s going to be two of those gps objects. I recommend checking if one’s already there before creating a new one earlier in the code. :+1:

2 Likes

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