Advertisement
Guest User

Easy Checkpoint Script (ROBLOX Lua)

a guest
Nov 27th, 2019
4,640
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. ---[CHECKPOINTS]-------------------------------------
  2. local Players = game:GetService('Players')
  3. local Obby = {}
  4. Obby.Checkpoints = {}
  5. Obby.Players = {}
  6. function Obby:Touched(Character, Part)
  7.     local Player = Players:GetPlayerFromCharacter(Character)
  8.     local PlayerInfo = self.Players[Player and Player.UserId or 0]
  9.     local CurrentLevel = PlayerInfo.Level
  10.     local NextLevel = CurrentLevel + 1
  11.     local StageInfo = self.Checkpoints[NextLevel] or {}
  12.     if StageInfo.Part and StageInfo.Part == Part and (Character.PrimaryPart.Position-Part.Position).Magnitude<=8 then
  13.         if tick() - PlayerInfo.LastLeveled < 3 and StageInfo.Level>=NextLevel then
  14.             PlayerInfo.Counts = PlayerInfo.Counts + 1
  15.             if PlayerInfo.Counts >= 3 then
  16.                 return Player:Kick('you went to next stage three times too fast ...')
  17.             end
  18.         end
  19.         if StageInfo.Level == NextLevel then
  20.             PlayerInfo.LastLeveled = tick()
  21.             PlayerInfo.Stage.Value = NextLevel
  22.             PlayerInfo.Level = NextLevel
  23.         end
  24.     end
  25. end
  26. function Obby:CreateStats(Player)
  27.     local Stats = Instance.new('Folder')
  28.     Stats.Name = 'leaderstats'
  29.     Stats.Parent = Player
  30.     local Level = Instance.new('IntValue')
  31.     Level.Name = 'Stage'
  32.     Level.Value = 0
  33.     Level.Parent = Stats
  34.     local Data = self.Players[Player.UserId]
  35.     Level.Value = Data.Level
  36.     Data.Stage = Level
  37. end
  38. function Obby.SetupCharacter(Character)
  39.     wait()
  40.     Character:WaitForChild'HumanoidRootPart'
  41.     local Player = Players:GetPlayerFromCharacter(Character)
  42.     local PlayerInfo = Obby.Players[Player.UserId]
  43.     local Checkpoint = Obby.Checkpoints[PlayerInfo.Level]
  44.     if Checkpoint then
  45.         Character:SetPrimaryPartCFrame(CFrame.new(Checkpoint.Part.Position) * CFrame.new(0,3,0))
  46.     end
  47. end
  48. function Obby.SetupPlayer(Player)
  49.     local Data = Obby.Players[Player.UserId]
  50.     Obby.Players[Player.UserId] = Data or {
  51.         ['Level'] = 0;
  52.         ['LastLeveled'] = 0;
  53.         ['Counts'] = 0;
  54.     };
  55.     local Character = Player.Character or Player.CharacterAdded:Wait()
  56.     Obby.SetupCharacter(Character)
  57.     Player.CharacterAdded:Connect(Obby.SetupCharacter)
  58.     return not Data and Obby:CreateStats(Player)
  59. end
  60. function Obby.RemovePlayer(Player)
  61.     local Data = Obby.Players[Player.UserId]
  62.     if Data then
  63.         Obby.Players[Player.UserId] = nil
  64.     end
  65. end
  66. function Obby:SetupCheckpoint(Part)
  67.     local Level = tonumber(Part.Name)
  68.     self.Checkpoints[Level] = {
  69.         ['Part'] = Part;
  70.         ['Level'] = Level;
  71.     }
  72.     Part.Touched:Connect(function(Hit)
  73.         local Character = Hit and Hit.Parent
  74.         local Humanoid = Character and Character:FindFirstChildOfClass'Humanoid'
  75.         if Humanoid and Humanoid.Health > 0 then
  76.             self:Touched(Character, Part)
  77.         end
  78.     end)
  79. end
  80. Players.PlayerAdded:Connect(Obby.SetupPlayer)
  81. Players.PlayerRemoving:Connect(Obby.RemovePlayer)
  82. for _, Player in next, Players:GetPlayers() do
  83.     Obby.SetupPlayer(Player)
  84. end
  85. local Checkpoints = workspace.Checkpoints
  86. for _, Checkpoint in next, Checkpoints:GetChildren() do
  87.     Obby:SetupCheckpoint(Checkpoint)
  88. end
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement