Problems with leaderboard timer

Hey together!

I am really new to programming in general but really have no experience in LUA. I started working through some of Roblox’ scripting tutorial videos. I have created a game where bombs rain down from random location above the map. To familiarise myself a little quicker with LUA, I started adding small features to my game. Now I want the leaderboard of my game to display the seconds my character has lived for.

I have started off by making myself a “LeaderboardGiver” script:

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Parent = leaderstats
	seconds.Value = 0	
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

Then I’ve added a second script, that’s supposed to keep track on when the player dies and when he respawns. I have started off by trying to make the Value of “Seconds” go up every second with this script:

local function Count(player)
	player.CharacterAdded:Connect(function(character)
		player = game.Players:GetPlayerFromCharacter(character)
		local secondsCount = player.leaderstats.Seconds.Value
		while character.Humanoid.Health > 0 do
			secondsCount = secondsCount + 1
			wait(1)
		end
	end)
end

game.Players.PlayerAdded:Connect(Count)

now upuntil line 5 everythign works. If I add a print(“player.Name”) my name is printed when my character spawns. It even outputts “0” when printing (“secondsCount”)

When I run the script shown above I expect that the “Seconds” value in my leaderboard would now increase by 1 every second but it doesn’t. There is no error message.

Is there something obviously wrong? Am I somehow not allowed to change the value?

Thanks and nice to be here!

3 Likes

Second script, line 5. It may be stuck at 0 because it only increases by one if it’s above 0. I think that’s your problem.

A quick an easy fix is when the player respawns, the starting value is 1, not 0.

1 Like

Thanks for the reply but it sadly didn’t work.

Even when I tried doing it like this:

local function Count(player)
	player.CharacterAdded:Connect(function(character)
		player = game.Players:GetPlayerFromCharacter(character)
		local secondsCount = player.leaderstats.Seconds.Value
		while true do
			secondsCount = secondsCount + 1
			wait(1)
		end
	end)
end

game.Players.PlayerAdded:Connect(Count)

it didn’t work.

Cheers

1 Like

Is this the entirety of the contents of the script?

1 Like

Yes that’s everything I’ve added so far.

You are overwriting the original player with the character of the player thats the problem

Edit: oof nevermind, misread the function. @Y35X reply below is correct, mark it as the solution

1 Like

Ohh! Nevermind, I see the issue! You are changing the variable value, not the actual leaderstat value. Try this instead:

local function Count(player)
	player.CharacterAdded:Connect(function(character)
		player = game.Players:GetPlayerFromCharacter(character)
		local secondsCount = player.leaderstats.Seconds.Value
		repeat
			player.leaderstats.Seconds.Value = secondsCount
                        secondsCount = secondsCount + 1
			wait(1)
		until character.Humanoid.Health > 0
	end)
end

game.Players.PlayerAdded:Connect(Count)

You were changing the variable value, but not actually updating the leaderstat. You could’ve also set secondsCount to player.leaderstats.Seconds and then done:

secondsCount.Value = secondsCount.Value + 1

Either way, both of these should work, variables on Lua gave me a headache for a while too when I was first learning the language.

2 Likes

It worked! Thanks a lot!

I’ve realized, that I can do all of this in one script:

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Parent = leaderstats
	seconds.Value = 0
	player.CharacterAdded:Connect(function(character)
		player = game.Players:GetPlayerFromCharacter(character)
		local secondsCount = player.leaderstats.Seconds.Value
		secondsCount = 0
		repeat
			player.leaderstats.Seconds.Value = secondsCount
			secondsCount = secondsCount+1
			wait(1)
		until character.Humanoid.Health < 1
	end)	
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

This works flawlessly now.

Altough I don’t quite understand why. You wrote that:

You were changing the variable value, but not actually updating the leaderstat.

Is this a general problem of the while true do loops? I’ve spend several minutes looking at the code now and I don’t really see what fixed it.

Here’s a quick rundown of why the value wasn’t changing but the variable was:

You set secondsCount to player.leaderstats.Seconds.Value, and while at first you might go “well now if I change the variable, the leaderstats value will also change”, but that is incorrect. See, you set the variable to the value of Seconds. So if that value is a numberValue, secondsCount isn’t a shortcut for describing the leaderstat value, but instead the variable simply becomes that number.

So if Seconds was 9, secondsCount doesn’t become a shortcut for player.leaderstats.Seconds.Value, secondsCount becomes 9.

On the contrary, if you make secondsCount player.leaderstats.Seconds, it does become a shortcut for the leaderstat value. The “value” to get out of this variable is a hierarchy leading to the leaderstat that you want. You’re not grabbing only a value, thus the variable becomes a shortcut for the leaderstat.

This is why secondsCount.Value = secondsCount.Value + 1 works if you define secondsCount as player.leaderstats.Seconds, because now you’re not just changing a value that a variable describes, but you are changing the value of the leaderstat.

I’m not the best at teaching so I hope this cleared things up.

2 Likes

Ooh now I understand! Sorry that took a while, it’s late ^^

Don’t worry I understood it quite well with your explination.

1 Like