generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
We use cookies to offer you a better experience. By using Roblox.com, you are agreeing to our Privacy and Cookie Policy.
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Level up Leaderboard

Previous Thread :: Next Thread 
geckohero17 is not online. geckohero17
Joined: 27 Dec 2009
Total Posts: 1342
21 Sep 2013 06:19 AM
It saves and loads most of the time, but sometimes a player's stats are reset to 0.
Is there anything I've left out or accidentally cut?

function onXPChanged(player, XP, Lvl)
if XP.Value>=Lvl.Value * 50 and Lvl.Value < script.LevelCap.Value then
XP.Value = XP.Value - Lvl.Value * 50
Lvl.Value = Lvl.Value + 1
end
end

function saveScore(player, score)
player:SaveNumber("Lvl", score)
end

function saveScore2(player, score)
player:SaveNumber("XP", score)
end

function saveScore3(player, score)
player:SaveNumber("GP", score)
end


function loadScore(player, clickCounter)
local score = player:LoadNumber("Lvl")

if score ~= 1 then
clickCounter.Value = score
else
print("Nothing to load/score was 0")
end

end

function loadScore2(player, clickCounter)
local score = player:LoadNumber("XP")

if score ~= 0 then
clickCounter.Value = score
else
print("Nothing to load/score was 0")
end

end

function loadScore3(player, clickCounter)
local score = player:LoadNumber("GP")

if score ~= 10 then
clickCounter.Value = score
else
print("Nothing to load/score was 0")
end

end

function onPlayerEntered(newPlayer)



local stats = Instance.new("IntValue")
stats.Name = "leaderstats"

local clicks = Instance.new("IntValue")
clicks.Name = "Lvl"
clicks.Value = 1

local clicks2 = Instance.new("IntValue")
clicks2.Name = "XP"
clicks2.Value = 0

local clicks3 = Instance.new("IntValue")
clicks3.Name = "GP"
clicks3.Value = 10


clicks.Parent = stats
clicks2.Parent = stats
clicks3.Parent = stats
stats.Parent = newPlayer

clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end)
clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) end)

newPlayer:WaitForDataReady()

loadScore(newPlayer, clicks)
loadScore2(newPlayer, clicks2)
loadScore3(newPlayer, clicks3)


newPlayer.CharacterAdded:connect(function(c)
c.Humanoid.MaxHealth = newPlayer.leaderstats.Lvl.Value * 10 + 50
end)
end

function onPlayerRemoving(player)
print("Attempting to save score for " .. player.Name)
local stats = player:FindFirstChild("leaderstats")
if (stats ~= nil) then
local clicks = stats:FindFirstChild("Lvl")
local clicks2 = stats:FindFirstChild("XP")
local clicks3 = stats:FindFirstChild("GP")
if (clicks ~= nil)and(clicks2 ~= nil)and(clicks3 ~= nil) then
saveScore(player, clicks.Value)
saveScore2(player, clicks2.Value)
saveScore3(player, clicks3.Value)
end
end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerRemoving)
Report Abuse
Project2015 is not online. Project2015
Joined: 11 Sep 2013
Total Posts: 384
21 Sep 2013 06:27 AM
function onPlayerEntered(player)
wait()
player:WaitForDataReady()
repeat wait() until player:FindFirstChild("leaderstats")
if player.DataReady then
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
local ScoreLoaded = player:LoadNumber(score[i].Name)
wait()
if ScoreLoaded ~= 0 then
score[i].Value = ScoreLoaded
end
end
end
end
end

function onPlayerLeaving(player)
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
player:SaveNumber(score[i].Name,score[i].Value)
end
end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerLeaving)
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image