I am a beginning scripter and I am working on this leader board script, and I want it to reset the TimeAlive value to zero every time a person dies, but also convert that number into XP by dividing it by ten. When I test the script on the server, the TimeAlive value continues to go up, even after death, and it never resets or converts to xp. I tried a debounce also, which is the alive variable, but that didn't work either. I am kind of stuck, so please help!
--script by Legobrain9275-- local alive = true game.Players.PlayerAdded:connect(function(player) stats = Instance.new("IntValue", player) stats.Name = "leaderstats" timealive = Instance.new("IntValue", stats) timealive.Name = "TimeAlive" xp = Instance.new("IntValue", stats) xp.Name = "XP" level = Instance.new("IntValue", stats) level.Name = "Level" while alive == true do wait(1) player.leaderstats.TimeAlive.Value = player.leaderstats.TimeAlive.Value + 1 end game.Players.PlayerAdded:connect(function(b) b.Character:WaitForChild("Humanoid").Died:connect(function() alive = false b.Character:GetPlayerFromCharacter(c) c.leaderstats.XP.Value = (c.leaderstats.XP.Value + (c.leaderstats.TimeAlive.Value/10)) c.leaderstats.TimeAlive.Value = 0 alive = true end) end) if player.leaderstats.XP.Value >= 1000 then player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1 player.leaderstats.XP.Value = 0 end end)
First of all, you will not reach that second "PlayerAdded" event because "alive" is never changed and so that loop you have there will run forever without stopping. Considering you were already in a "PlayerAdded" event a second one was not required. In the player object there is an event for detecting when the character model spawns. I.E when the player is "alive"
I believe that something like this was what you were looking for:
--script by Legobrain9275-- local alive = true game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local timealive = Instance.new("IntValue", stats) timealive.Name = "TimeAlive" local xp = Instance.new("IntValue", stats) xp.Name = "XP" local level = Instance.new("IntValue", stats) level.Name = "Level" player.CharacterAdded:connect(function(character) --Detects when the player respawns, perfect time to set alive to true alive = true character:WaitForChild("Humanoid").Died:connect(function() --The WaitForChild returns the instance of the humanoid alive = false player.leaderstats.XP.Value = player.leaderstats.XP.Value + (player.leaderstats.TimeAlive.Value/10) player.leaderstats.TimeAlive.Value = 0 end) end) while true do --Make sure this loop runs forever if alive then player.leaderstats.TimeAlive.Value = player.leaderstats.TimeAlive.Value + 1 end if player.leaderstats.XP.Value >= 1000 then player.leaderstats.Level.Value = player.leaderstats.Level.Value +1 player.leaderstats.XP.Value = 0 end wait(1) end end)
Hope this helps and works (I have tested and it does work for me)
Your script never makes it past line 22.
while alive == true do --This script goes on forever, and never allows you to move on wait(1) player.leaderstats.TimeAlive.Value = player.leaderstats.TimeAlive.Value + 1 end
In order to allow the script to go past that loop, you can use a spawn() function to execute the loop on another thread.
spawn( function() while alive == true do --This script goes on forever, and never allows you to move on wait(1) player.leaderstats.TimeAlive.Value = player.leaderstats.TimeAlive.Value + 1 end end)
Also, on line 24, I believe you were trying to connect to the event player.CharacterAdded
. List of Player events here.
Also, a few other things.
Line 18 while alive == true do
will run as long as the player is alive, however, once the player dies, it will stop running.
Also, b.Character:GetPlayerFromCharacter(c)
doesn't make sense, because we already have the player defined as player
.
Additionally, on line 34, this code: if player.leaderstats.XP.Value >= 1000 then
only runs once.
And, instead of using a Humaniod.Died event, we could use CharacterRemoving
--script by Legobrain9275-- local alive = true game.Players.PlayerAdded:connect(function(player) stats = Instance.new("IntValue", player) stats.Name = "leaderstats" timealive = Instance.new("IntValue", stats) timealive.Name = "TimeAlive" xp = Instance.new("IntValue", stats) xp.Name = "XP" level = Instance.new("IntValue", stats) level.Name = "Level" spawn( function() while true do wait(1) if alive then player.leaderstats.TimeAlive.Value = player.leaderstats.TimeAlive.Value + 1 if player.leaderstats.XP.Value >= 1000 then player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1 player.leaderstats.XP.Value = 0 end end end end) player.CharacterAdded:connect(function(b) alive = true end) player.CharacterRemoving:connect(function(b) alive = false player.leaderstats.XP.Value = (player.leaderstats.XP.Value + (player.leaderstats.TimeAlive.Value/10)) player.leaderstats.TimeAlive.Value = 0 end) end)