Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my leaderboard status not updating?

I am doing things a bit differently to prevent hacking, even though I have filtering enabled, make sure you read otherwise you are going to give me the wrong answer.

Rather than using leaderboardstats as the location to store data, I am simply using it as a means of displaying the values. The REAL values are located in ServerStorage. Here is my script that creates the data for players in both ServerStorage and then an empty leaderboard stat.

game.Players.PlayerAdded:connect(function(player)
	local entry = player.Name
	local model = Instance.new("Model", game.ServerStorage)
	local timeplayed = Instance.new("IntValue", game.ServerStorage.Model)
		timeplayed.Name = "Minutes"
	model.Name = entry
	local stats = Instance.new("IntValue")
		stats.Parent = player
		stats.Name = "leaderstats"
	local timeplayed2 = Instance.new("IntValue")
		timeplayed2.Parent = player.leaderstats
		timeplayed2.Name = "Minutes"
end)

Remember, everything in leaderstats is just mean to be like a GUI, its just meant to display the data, not actually be the important place that stores it.

Now, I have a separate script that is supposed to add 1 to the IntValue located in ServerStorage and then afterwards set the value of the IntValue in the leaderstats to the IntValue in ServerStorage

game.Players.PlayerAdded:connect(function(player)
	local entry = player.Name
	local data = game.ServerStorage:FindFirstChild(player.Name)
	assert(data, "PlayerData not created yet for `" .. player.Name .. "`")
	local minutesServerStorage = data.Minutes
	local minutesLeaderboards = player:WaitForChild("leaderstats").Minutes
	while true do wait()
		minutesServerStorage.Value = minutesServerStorage.Value + 1
		wait(1)
		 minutesLeaderboards.Value = game.ServerStorage[entry].Minutes.Value --Sync the Coins IntValue in ServerStorage with one in leaderstats	
		wait(1)
	end
end)				

When I test my game, I have both a serverstorage and leaderstats of the data, the problem seems to lie with the while true do not updating the IntValue in serverstorage and in leaderboards.

2 answers

Okay so, I literally just copied everything from the second script into the first and that made it worked, apparently I can't have two of these onplayeradded scripts or maybe one script runs before the other stuff gets made that is where a bug happens, idk.

Idea1: Used the Changed event.. Like seriously. By using a never ending loop, your not gonna help lag AT ALL. The changed event will react as soon as the object changes. Any object, any property. So this will allow you to sync it as soon as your scripts know its changed. This helps with lag and could make it more efficient

Idea 2: I doubt this is an issue btw. I think theres an issue with this, I don't know but i'll tell you anyway. Your using a loop on a server script designed to update for all clients. Not gonna happen. Heres a bit of detail. Every script in a roblox game is given a thread. On each thread only 1 function/line of code can be executed at a time. By constantly looping to check for values, your not giving the thread time for the other players stuff to execute.

This discussion is preserved as it was. New answers, comments, and votes are closed.