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?
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)
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.
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.