Advertisement
Guest User

brian42

a guest
Apr 27th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. game.Players.PlayerAdded:connect(function (player)
  2. -- Add the leaderboard stats to the player
  3. local leaderstats = Instance.new("Model")
  4. leaderstats.Name = "leaderstats"
  5. leaderstats.Parent = player
  6.  
  7. -- Add the level
  8. local level = Instance.new("IntValue")
  9. level.Name = "Level"
  10. level.Value = 1
  11. level.Parent = leaderstats
  12.  
  13. -- Can't save and load data to player right after they're added, have to wait to use this first
  14. if not player:WaitForDataReady() then
  15. return
  16. end
  17.  
  18. level.Value = player:LoadNumber("Level")
  19. end)
  20.  
  21. local checkpoints = workspace.Checkpoints
  22. for _, checkpoint in pairs(checkpoints:GetChildren()) do
  23. checkpoint.Touched:connect(function (touchedBy)
  24. local character = touchedBy.Parent
  25. local player = game.Players:playerFromCharacter(character)
  26. -- For some reason player can be nil sometimes b/c this gets called even when we don't touch it
  27. -- And make sure that we don't lower the player's level if they touch an earlier checkpoint
  28. if (player ~= nil and tonumber(checkpoint.Name) > player.leaderstats.Level.Value) then
  29. local level = tonumber(checkpoint.Name)
  30. player.leaderstats.Level.Value = level
  31. if player.DataReady then
  32. player:SaveNumber("Level", level)
  33. end
  34. end
  35. end)
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement