Leaderstats doesn't update in game, but does in Studio

Hello!

I have a weird problem that I can’t figure out the solution to.

Basically, I’ve made a function that changes a player’s leaderstat values and I’ve put this function into a loop. In Studio, this system works fine. Although, when I test it in game, it doesn’t work at all.

Here’s the code:

local dataMod = require(game.ServerScriptService.Modules.DataModule)

local function updateLeaderStats(plr)
	local gold = dataMod:GetStat(plr, "Gold")
	plr.leaderstats.Gold.Value = gold
end

local function autoUpdateLeaderBoard()
	while true do
		wait()
		local allPlayers = game.Players:GetChildren()
		for i,v in pairs(allPlayers) do
			updateLeaderStats(v)
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	dataMod:setupPlayerData(plr)
end)

spawn(autoUpdateLeaderBoard) 

Again, it works fine in Studio, but doesn’t work in game.

Any explanations as to why this is occurring?

EDIT: Changed some code

2 Likes

have you tried waiting for the modules? and were there any errors in console?

2 Likes

First of all, this:

while true do
	wait()
	local allPlayers = game.Players:GetChildren()
	for i,v in pairs(allPlayers) do
		updateLeaderStats(v)
	end
end

This can cause major lag. Why do you need to update the leaderboard every hundredth of a second (look under wait and what it defaults to)? You should at least wait one second. Doing this so many times a second is not good.

That’s all I have to offer now, I hope you find your solution!

2 Likes

There are no errors in the console at all. I don’t think the module is the problem.

have you tried debugging your script with prints and see where it stops?

No, actually, that’s a great idea. I don’t have the best ideas when it comes to debugging.

For some reason, changing the wait period made the system work. Thanks for the suggestion!

2 Likes

The Issue


You’re probably encountering a problem with the module. This code does not contain any errors apart from the module itself. If we could take a look into the dataMod, we could possibly aid your solution.

The loop may cause lag as previously mentioned by @DatOneRandomDude, alternative solutions could be binding it to a signal when a value changes, or extending the interval. The lag depends on how expensive the code is. I recommend using an event rather than loops to update the stats(this could be a little hassle to create). If not, extend the loop interval to X seconds rather than tens of milliseconds.

Summary of The Current Analysis


No flaw detected in the code; excluding the loop that could cause lag. script.Parent is unknown.

My guess is that the module has something jammed that prevents it from working as intended.

1 Like

This likely isn’t a real fix but instead just makes the bug reproduce less frequently. If I had to guess what is going on, calling dataMod:GetStat() before dataMod:setupPlayerData() is finished causes the error.

4 Likes

dataMod:GetStat() is a yielding function. If GetStat() is called before playerData is available, then it will loop until the player’s data is available.

EDIT: Actually, I don’t think this is considered yielding.

2 Likes