Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- game.Players.PlayerAdded:connect(function (player)
- -- Add the leaderboard stats to the player
- local leaderstats = Instance.new("Model")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- -- Add the level
- local level = Instance.new("IntValue")
- level.Name = "Level"
- level.Value = 1
- level.Parent = leaderstats
- -- Can't save and load data to player right after they're added, have to wait to use this first
- if not player:WaitForDataReady() then
- return
- end
- level.Value = player:LoadNumber("Level")
- end)
- local checkpoints = workspace.Checkpoints
- for _, checkpoint in pairs(checkpoints:GetChildren()) do
- checkpoint.Touched:connect(function (touchedBy)
- local character = touchedBy.Parent
- local player = game.Players:playerFromCharacter(character)
- -- For some reason player can be nil sometimes b/c this gets called even when we don't touch it
- -- And make sure that we don't lower the player's level if they touch an earlier checkpoint
- if (player ~= nil and tonumber(checkpoint.Name) > player.leaderstats.Level.Value) then
- local level = tonumber(checkpoint.Name)
- player.leaderstats.Level.Value = level
- if player.DataReady then
- player:SaveNumber("Level", level)
- end
- end
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement