Problem with Changed Function

Hi, I’m currently trying to make a time script that checks the leaderstats and award a badge. While doing that I came across a problem. It doesn’t print at all even though I have the curtain amount of time needed for it to print. Here is the script:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local stats = Instance.new("IntValue")
	stats.Parent = leaderstats
	stats.Name = "Time"
	
	local success, err = pcall(function()
		data = ds1:GetAsync(plr.UserId)
	end)
	
	if success then
		stats.Value = data
	end 
	
	stats.Changed:Connect(function()
		wait(1)
		
		stats.Value = stats.Value + 1
		
		if stats.Value > 0 then
			
			print("test") ---Where it's supposed to print.
			
			
		end
		
		
	end)
		
	
	
	
	
end)

Nothing in the output. I’m not sure if it’s my fault or not, but any help would be appreciated.

I’m pretty sure the issue would lie in whatever is changing the value. There isn’t really enough info here to be super helpful, but a common issue people hit is that they are trying to detect a change with a server script, that was made by a local script. That doesn’t work because the local script will only change it on the machine it’s running on and not replicate to the server. If that’s your issue, you would need to use a remote event. If that’s not the issue, more information about what is changing it would be needed to help you.

I’m currently using a serverscript, so I don’t think that’s the problem.

What’s changing the value? Nothing in that script is changing it after the connection.

stats.Changed:Connect(function()
		wait(1)
		
		stats.Value = stats.Value + 1

This here, detects a change then adds one to the leaderstat

If there is no other place that changes it, then you don’t have anything starting the .changed loop. Though, if you are just wanting to increment it every second, why not just use a while loop?

I tried using a while true loop before, but it just ruins the time. Goes from 221 to 223 to 219 and so on.

That shouldn’t happen with a while loop. Are you changing it somewhere else too?

Try running this code instead

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local stats = Instance.new("IntValue")
	stats.Parent = leaderstats
	stats.Name = "Time"
	
	local success, err = pcall(function()
		data = ds1:GetAsync(plr.UserId)
	end)
	
	if success then
		stats.Value = data
	end 
	
	while true do
		wait(1)
		
		stats.Value = stats.Value + 1
		print(stats.Value)
		
		if stats.Value > 0 then
			print("test") ---Where it's supposed to print.
		end
	end
end)

Still going a little fast and changing to random numbers, there’s no other script besides that one that changes the value though

Try this and see if the second column is counting normally

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local stats = Instance.new("IntValue")
	stats.Parent = leaderstats
	stats.Name = "Time"
	
	local success, err = pcall(function()
		data = ds1:GetAsync(plr.UserId)
	end)
	
	if success then
		stats.Value = data
	end 
	
	
	local test = stats.Value
	while true do
		wait(1)
		
		test = test + 1
		stats.Value = stats.Value + 1
		print(stats.Value,test)
		
		if stats.Value > 0 then
			print("test") ---Where it's supposed to print.
		end
	end
end)
1 Like

The second column in working printing normally, how would i get it to work so it updates the leaderstats also?

With that working normally, it means there is probably something you are missing that’s messing with the stats values. Because other than just being a different variable, they are doing the same thing. So you need to find whatever is messing it up.

Would you think it’s the data store script that saves the data?

I don’t think it’s going to be that unless you are writing to the variable in that script somewhere too. You can try changing the name and/or location of the stat object to see if any other script throws an error if you can’t find anything that is writing to it.

I have a leaderboard somewhere that checks the leaderstats, not sure if that would be the problem.

Not sure that I understand what’s wrong in here.
Your .Changed function will fire only when a property in the stats changes. But what you did here was that when it changes, increase it by 1. And that will run infinitely because every second it’ll change again.

If the stats doesn’t change, it won’t run. When it does change, it’ll run every second and increase the stats value by 1.

If you want to make a timer, you can do something like this instead:

local length = 10
for i = 0,10 do -- I is equal to 0,  increase it by 1 and run this code until i is 10
	wait(1)
	print(i) --Print amount of seconds
--Or backwards
for i = length,0,-1 do -- I is equal to 10,  decrease it by 1 and run this code until i is 0
	wait(1)
	print(i) --Print amount of seconds
end
1 Like

I’m not trying to make a timer though, I’m trying to make a script that tells the player how much time they spent in-game.

Oh, then you can do something like this:

while plr:IsDescendantOf(game:GetService("Players")) do --If the player is in the players (didn't leave the game)
	stats.Value = stats.Value + 1 --Increase the value
	wait(1) --Wait a second
end

Every second the value will increase as long as the player is in the game.

1 Like

The time is still going a little fast, not sure why

Are you sure? That should run about every second.