My checkpoint script isn’t working, I don’t know what I am doing incorrect.
I made a simple checkpoint script, but when I die it does not respawn me back to my checkpoint, it resets it back to the default spawn. Everything in the script seemed correct
local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
local defaultSpawn = checkPoints:WaitForChild("1")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local HRP = character:WaitForChild("HumanoidRootPart")
HRP.CFrame = defaultSpawn.CFrame * CFrame.new(0,1,0)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Value = 1
Stage.Parent = leaderstats
end)
end)
Script under the spawnPoint
local checkPoint = script.Parent
local checkPointNum = checkPoint:WaitForChild("CheckpointNum").Value
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Humanoid.Health > 0 then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local HRP = player.Character:WaitForChild("HumanoidRootPart")
if player.leaderstats.Stage.Value < checkPointNum then
print("Player"..player.Name.."Has reached level"..checkPointNum)
player.leaderstats.Stage.Value = checkPointNum
HRP.CFrame = checkPoint.CFrame * CFrame.new(0,1,0)
end
end
end
end)
local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
local defaultSpawn = checkPoints:WaitForChild("1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Value = 1
Stage.Parent = leaderstats
Stage.Changed:Connect(function() -- this fires every time your stage value in leaderstats get changed
defaultSpawn = checkPoints:WaitForChild(Stage.Value)
end)
player.CharacterAdded:Connect(function(character)
local HRP = character:WaitForChild("HumanoidRootPart")
HRP.CFrame = defaultSpawn.CFrame * CFrame.new(0,1,0)
end)
end)
``
local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Value = 1
Stage.Parent = leaderstats
leaderstats.Parent = player
player.CharacterAdded:Connect(function(character)
character:PivotTo(checkPoints:WaitForChild(tostring(Stage.Value)):GetPivot() * CFrame.new(0, 1, 0))
end)
end)
local checkPoint = script.Parent
local checkPointNum = checkPoint:WaitForChild("CheckpointNum").Value
checkPoint.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Humanoid.Health > 0 then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
if player.leaderstats.Stage.Value < checkPointNum then
print(string.format("Player %s has reached level %i!", player.Name, checkPointNum))
player.leaderstats.Stage.Value = checkPointNum
hit.Parent:PivotTo(checkPoint:GetPivot() * CFrame.new(0,1,0))
end
end
end
end)