local StageData = game:GetService("DataStoreService"):GetDataStore("StageData")
local Checkpoints = workspace:WaitForChild("Checkpoints"):GetChildren()
function GoToCheckpoint(character, stage)
local rootPart = character:WaitForChild("HumanoidRootPart")
repeat wait(0.00) until rootPart
for i, checkpoint in pairs(Checkpoints) do
print("Checkpoint" .. tostring(stage))
if ("Checkpoint" .. tostring(stage)) == checkpoint.Name then
print("Checkpoint found")
rootPart.CFrame = checkpoint.CFrame * CFrame.new(0,1,0)
break
-- Stop looping since we found the checkpoint
end
end
end
game:GetService("Players").PlayerAdded:Connect(function(player)
wait(0.1)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local stage = Instance.new("IntValue", leaderstats)
stage.Name = "Stage"
stage.Value = StageData:GetAsync(player.UserId) or 1
-- Retrieve the player's data, but if there is none, start the player on stage 1
player.CharacterAdded:Connect(function(character)
wait(0.1)
-- The function we're connecting to this event will be called everytime the player's character spawns
GoToCheckpoint(character, stage.Value)
end)
end)
for i, checkpoint in pairs(Checkpoints) do
checkpoint.Touched:Connect(function(touch)
-- Touched is an event that is fired when the object is touched
local hum = touch:FindFirstChild("Plr")
-- If a part in a model touches the part, touch = that part not the model
if hum then
-- The thing that touched the checkpoint is alive
-- Whatever touched the checkpoint is a player
local leaderstats = game.Players[hum.Value]:WaitForChild("leaderstats")
local stage = leaderstats:WaitForChild("Stage")
-- Find the stage leader stat
if (tonumber(checkpoint.Name:match("%d+"))-stage.Value) == 1 then
-- match() finds certain parts in a string
-- So when we do match("%d"), we're finding the numbers in a string
-- This is the player's next checkpoint
-- This makes it so player's can't skip checkpoints
-- or lose progress if they accidently go backwards
stage.Value = stage.Value + 1
end
end
end)
end
game:GetService("Players").PlayerRemoving:Connect(function(player)
StageData:SetAsync(player.UserId, player.leaderstats.Stage.Value)
-- Saves the player's data when they leave the game
end)
when i spawn in, i get spawned in the void, and when i died the checkpoint registers and i get spawned in like normal.
Easy fix, all you have to do is make it so the GoToCheckpoint function is run before calling the character-added connection. The reason is that by the time your code gets to that point the character is already spawned meaning the function won’t trigger meaning you won’t be sent to the checkpoint.
ame:GetService("Players").PlayerAdded:Connect(function(player)
wait(0.1)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local stage = Instance.new("IntValue", leaderstats)
stage.Name = "Stage"
stage.Value = StageData:GetAsync(player.UserId) or 1
-- Retrieve the player's data, but if there is none, start the player on stage 1
local character = workspace:WaitForChild(player.Name)
GoToCheckpoint(character, stage.Value)
--player.CharacterAdded:Connect(function(character)
-- wait(0.1)
-- -- The function we're connecting to this event will be called everytime the player's character spawns
--end)
end)
You still need the other character added code you just needed the new code to Initialize that process before it could trigger
do this
game:GetService("Players").PlayerAdded:Connect(function(player)
wait(0.1)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local stage = Instance.new("IntValue", leaderstats)
stage.Name = "Stage"
stage.Value = StageData:GetAsync(player.UserId) or 1
-- Retrieve the player's data, but if there is none, start the player on stage 1
local character = workspace:WaitForChild(player.Name)
GoToCheckpoint(character, stage.Value)
player.CharacterAdded:Connect(function(character)
wait(0.1)
-- The function we're connecting to this event will be called everytime the player's character spawns
end)
end)
game:GetService("Players").PlayerAdded:Connect(function(player)
wait(0.1)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local stage = Instance.new("IntValue", leaderstats)
stage.Name = "Stage"
stage.Value = StageData:GetAsync(player.UserId) or 1
-- Retrieve the player's data, but if there is none, start the player on stage 1
local character = workspace:WaitForChild(player.Name)
GoToCheckpoint(character, stage.Value)
player.CharacterAdded:Connect(function(character)
wait(0.1)
GoToCheckpoint(character, stage.Value)
-- The function we're connecting to this event will be called everytime the player's character spawns
end)
end)