Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Checkpoint = workspace.Checkpoint
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local PlayerInGame = {}
- local InCooldown = {}
- local PlayerStage = {}
- local DeltaTime = 0
- local WalkSpeed = 16
- local function UpdateDeltaTime()
- RunService.Heartbeat:Connect(function(deltaTime)
- DeltaTime = deltaTime
- end)
- end
- local function CreateLeaderstats(player : Player)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- return leaderstats
- end
- local function CharacterAdded(player : Player,character : Model,stage : number)
- repeat task.wait() until character:IsDescendantOf(workspace)
- local CF = Checkpoint[stage]:GetPivot() * CFrame.new(0,5,0)
- character:PivotTo(CF)
- end
- local function PlayerAdded(player : Player)
- local leaderstats = CreateLeaderstats(player)
- local IntValue = Instance.new("IntValue")
- IntValue.Name = "Stage"
- IntValue.Parent = leaderstats
- IntValue.Value = 1
- PlayerInGame[player] = player
- PlayerStage[player] = IntValue.Value
- InCooldown[player] = false
- player.CharacterAdded:Connect(function(model)
- CharacterAdded(player,model,PlayerStage[player])
- end)
- end
- local function PlayerRemoving(player : Player)
- PlayerInGame[player] = nil
- InCooldown[player] = nil
- PlayerStage[player] = nil
- end
- local function SetCheckpoint(player : Player , n : number)
- PlayerStage[player] = n
- end
- local function RemoveCooldown(player : Player)
- InCooldown[player] = nil
- end
- local function SetCooldown(Player : Player , time : number)
- task.spawn(function()
- InCooldown[Player] = true
- local current = time
- local character = Player.Character or Player.CharacterAdded:Wait()
- local Humanoid: Humanoid = character:FindFirstChild("Humanoid")
- if not Humanoid then
- RemoveCooldown(Player)
- return
- end
- while current > 0 do
- current -= DeltaTime
- RunService.Heartbeat:Wait()
- local Distance_Y = math.abs(character.PrimaryPart.Position.Y - Checkpoint[PlayerStage[Player] + 1].Position.Y)
- repeat task.wait() until Humanoid.FloorMaterial ~= Enum.Material.Air or Distance_Y < character:GetExtentsSize().Magnitude * 2
- end
- RemoveCooldown(Player)
- end)
- end
- local function CheckpointTouched(checkpoint : BasePart,hit : Instance)
- local Player = Players:GetPlayerFromCharacter(hit:FindFirstAncestorWhichIsA("Model"))
- if Player then
- local Character = Player.Character
- if not Character then
- return
- end
- local PlayerCurrentStage = PlayerStage[Player]
- local StageNumber = tonumber(checkpoint.Name)
- if PlayerCurrentStage == StageNumber - 1 then
- if InCooldown[Player] then
- return
- end
- SetCheckpoint(Player,StageNumber)
- if not Checkpoint:FindFirstChild(tostring(PlayerStage[Player] + 1)) then
- return
- end
- local CooldownTimePreferred = (Checkpoint[PlayerStage[Player]].Position - Checkpoint[PlayerStage[Player] + 1].Position).Magnitude / WalkSpeed
- SetCooldown(Player,CooldownTimePreferred)
- end
- end
- end
- local function InitializeCheckpoint(Checkpoint : BasePart)
- Checkpoint.Touched:Connect(function(...)
- CheckpointTouched(Checkpoint,...)
- end)
- end
- local function Init()
- table.foreach(Checkpoint:GetChildren(),function(i,checkpoint)
- InitializeCheckpoint(checkpoint)
- end)
- Players.PlayerAdded:Connect(PlayerAdded)
- Players.PlayerRemoving:Connect(PlayerRemoving)
- UpdateDeltaTime()
- end
- Init()
Advertisement
Add Comment
Please, Sign In to add comment