luchitasin9

Checkpoint Script

Aug 18th, 2022 (edited)
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | Source Code | 0 0
  1. local STAT_NAME = "Stage"
  2. local PREVENT_SKIPPING = true
  3.  
  4. local checkpoints = {}
  5.  
  6. local i = 1
  7. while true do
  8.     local checkpoint = Workspace:FindFirstChild("Checkpoint " .. i, true)
  9.     if not checkpoint then print("Last Checkpoint : " .. i-1) break end
  10.     table.insert(checkpoints, checkpoint)
  11.     i = i + 1
  12. end
  13.  
  14. game.Players.PlayerAdded:connect(function(player)
  15.     local leaderstats = player:FindFirstChild("leaderstats") or Instance.new("Model", player)
  16.     leaderstats.Name = "leaderstats"
  17.  
  18.     local checkpointStat = Instance.new("IntValue", leaderstats)
  19.     checkpointStat.Name = STAT_NAME
  20.     checkpointStat.Value = 1
  21.  
  22.     player.CharacterAdded:connect(function(character)
  23.         local goto = checkpoints[checkpointStat.Value]
  24.         if goto then
  25.             repeat wait() until character.Parent
  26.             character:MoveTo(goto.Position)
  27.         else
  28.             warn("Checkpoint " .. checkpointStat.Value .. " not found")
  29.         end
  30.     end)
  31. end)
  32.  
  33. for index, checkpoint in ipairs(checkpoints) do
  34.     checkpoint.Touched:connect(function(hit)
  35.         local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  36.         if not player then return end
  37.         local humanoid = hit.Parent:FindFirstChild("Humanoid")
  38.         if not humanoid or humanoid.Health <= 0 then return end
  39.         local leaderstats = player:FindFirstChild("leaderstats")
  40.         if not leaderstats then return end
  41.         local checkpointStat = leaderstats:FindFirstChild(STAT_NAME)
  42.         if not leaderstats then return end
  43.  
  44.         if (PREVENT_SKIPPING and checkpointStat.Value + 1 == index) or (not PREVENT_SKIPPING and checkpointStat.Value < index) then
  45.             checkpointStat.Value = index
  46.         end
  47.     end)
  48. end
Add Comment
Please, Sign In to add comment