ITS_SELINASOON

Checkpoint V2

Jun 20th, 2022
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. local Checkpoint = workspace.Checkpoint
  2. local Players = game:GetService("Players")
  3. local RunService = game:GetService("RunService")
  4.  
  5. local PlayerInGame = {}
  6. local InCooldown = {}
  7. local PlayerStage = {}
  8.  
  9. local DeltaTime = 0
  10. local WalkSpeed = 16
  11.  
  12. local function UpdateDeltaTime()
  13.     RunService.Heartbeat:Connect(function(deltaTime)
  14.         DeltaTime = deltaTime
  15.     end)
  16. end
  17.  
  18. local function CreateLeaderstats(player : Player)
  19.     local leaderstats = Instance.new("Folder")
  20.     leaderstats.Name = "leaderstats"
  21.     leaderstats.Parent = player
  22.    
  23.     return leaderstats
  24. end
  25.  
  26. local function CharacterAdded(player : Player,character : Model,stage : number)
  27.     repeat task.wait() until character:IsDescendantOf(workspace)
  28.     local CF = Checkpoint[stage]:GetPivot() * CFrame.new(0,5,0)
  29.     character:PivotTo(CF)
  30. end
  31.  
  32. local function PlayerAdded(player : Player)
  33.     local leaderstats = CreateLeaderstats(player)
  34.    
  35.     local IntValue = Instance.new("IntValue")
  36.     IntValue.Name = "Stage"
  37.     IntValue.Parent = leaderstats
  38.     IntValue.Value = 1
  39.    
  40.     PlayerInGame[player] = player
  41.     PlayerStage[player] = IntValue.Value
  42.     InCooldown[player] = false
  43.    
  44.     player.CharacterAdded:Connect(function(model)
  45.         CharacterAdded(player,model,PlayerStage[player])
  46.     end)
  47. end
  48.  
  49. local function PlayerRemoving(player : Player)
  50.     PlayerInGame[player] = nil
  51.     InCooldown[player] = nil
  52.     PlayerStage[player] = nil
  53. end
  54.  
  55. local function SetCheckpoint(player : Player , n : number)
  56.     PlayerStage[player] = n
  57. end
  58.  
  59. local function RemoveCooldown(player : Player)
  60.     InCooldown[player] = nil
  61. end
  62.  
  63. local function SetCooldown(Player : Player , time : number)
  64.     task.spawn(function()
  65.         InCooldown[Player] = true
  66.         local current = time
  67.         local character = Player.Character or Player.CharacterAdded:Wait()
  68.         local Humanoid: Humanoid = character:FindFirstChild("Humanoid")
  69.        
  70.         if not Humanoid then
  71.             RemoveCooldown(Player)
  72.             return
  73.         end
  74.        
  75.         while current > 0 do
  76.             current -= DeltaTime
  77.             RunService.Heartbeat:Wait()
  78.             local Distance_Y = math.abs(character.PrimaryPart.Position.Y - Checkpoint[PlayerStage[Player] + 1].Position.Y)
  79.             repeat task.wait() until Humanoid.FloorMaterial ~= Enum.Material.Air or Distance_Y < character:GetExtentsSize().Magnitude * 2
  80.         end
  81.        
  82.         RemoveCooldown(Player)
  83.     end)
  84. end
  85.  
  86. local function CheckpointTouched(checkpoint : BasePart,hit : Instance)
  87.     local Player = Players:GetPlayerFromCharacter(hit:FindFirstAncestorWhichIsA("Model"))
  88.    
  89.     if Player then
  90.         local Character = Player.Character
  91.        
  92.         if not Character then
  93.             return
  94.         end
  95.        
  96.         local PlayerCurrentStage = PlayerStage[Player]     
  97.         local StageNumber = tonumber(checkpoint.Name)
  98.        
  99.         if PlayerCurrentStage == StageNumber - 1 then
  100.             if InCooldown[Player] then
  101.                 return
  102.             end
  103.            
  104.             SetCheckpoint(Player,StageNumber)
  105.            
  106.             if not Checkpoint:FindFirstChild(tostring(PlayerStage[Player] + 1)) then
  107.                 return
  108.             end
  109.            
  110.             local CooldownTimePreferred = (Checkpoint[PlayerStage[Player]].Position - Checkpoint[PlayerStage[Player] + 1].Position).Magnitude / WalkSpeed
  111.            
  112.             SetCooldown(Player,CooldownTimePreferred)
  113.         end
  114.     end
  115. end
  116.  
  117. local function InitializeCheckpoint(Checkpoint : BasePart)
  118.     Checkpoint.Touched:Connect(function(...)
  119.         CheckpointTouched(Checkpoint,...)
  120.     end)
  121. end
  122.  
  123. local function Init()
  124.     table.foreach(Checkpoint:GetChildren(),function(i,checkpoint)
  125.         InitializeCheckpoint(checkpoint)
  126.     end)
  127.  
  128.     Players.PlayerAdded:Connect(PlayerAdded)
  129.     Players.PlayerRemoving:Connect(PlayerRemoving)
  130.    
  131.     UpdateDeltaTime()
  132. end
  133.  
  134. Init()
Advertisement
Add Comment
Please, Sign In to add comment