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.
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
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
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.
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…
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.