I’ve been making a checkpoint system, and I have tried many methods to teleport the player to the checkpoint. I even tried moving the player to another position regardless of checkpoints but still didn’t work.
Here’s the script:
local Services = {
Players = game:GetService("Players");
ServerStorage = game:GetService("ServerStorage");
DataStoreService = game:GetService("DataStoreService");
}
local DataStores = {
StageDataStore = Services.DataStoreService:GetDataStore("StageData");
}
local PartsFolder = workspace:WaitForChild("Parts")
local ServerLibs = Services.ServerStorage:WaitForChild("Libs")
local Modules = {
PartHandler = require(ServerLibs:WaitForChild("PartHandler"));
}
Services.Players.PlayerAdded:Connect(function(player)
local DataFolder = Instance.new("Folder", player)
DataFolder.Name = "Data"
local CurrentStage = Instance.new("NumberValue", DataFolder)
CurrentStage.Name = "Stage"
local Leaderstats = Instance.new("Folder", player)
Leaderstats.Name = "leaderstats"
for index, value in pairs(PartsFolder:GetDescendants()) do
Modules.PartHandler.new(player, value)
end
local Data
local success, errormessage = pcall(function()
Data = DataStores.StageDataStore:GetAsync(player.UserId)
end)
if success then
CurrentStage.Value = Data
end
for index, value in pairs(DataFolder:GetChildren()) do
local Cloned = value:Clone()
Cloned.Parent = Leaderstats
end
end)
Services.Players.PlayerRemoving:Connect(function(player)
local Data = player.Data.Stage.Value
DataStores.StageDataStore:SetAsync(player.UserId, Data)
end)
Services.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.HumanoidRootPart.CFrame = CFrame.new(500, 50, 500) -- This didn't work.
local CurrentStage = player.Data.Stage.Value
if CurrentStage > 0 then
for index, value in pairs(PartsFolder:GetDescendants()) do
local Stage = value:GetAttribute("Stage")
if value.Name == "Checkpoint" and Stage == CurrentStage and (value:IsA("BasePart") or value:IsA("MeshPart")) then
local CharacterCFrame = character:WaitForChild("HumanoidRootPart")
CharacterCFrame.CFrame = value.CFrame
end
end
end
end)
end)
Hello, I’ve run into this issue myself at times and the cause of the issue seems to be that the game sets the position of the player to the default spawnpoint after the player.CharacterAdded event fires.
local Services = {
Players = game:GetService("Players");
ServerStorage = game:GetService("ServerStorage");
DataStoreService = game:GetService("DataStoreService");
}
local DataStores = {
StageDataStore = Services.DataStoreService:GetDataStore("StageData");
}
local PartsFolder = workspace:WaitForChild("Parts")
local ServerLibs = Services.ServerStorage:WaitForChild("Libs")
local Modules = {
PartHandler = require(ServerLibs:WaitForChild("PartHandler"));
}
Services.Players.PlayerAdded:Connect(function(player)
local DataFolder = Instance.new("Folder", player)
DataFolder.Name = "Data"
local CurrentStage = Instance.new("NumberValue", DataFolder)
CurrentStage.Name = "Stage"
local Leaderstats = Instance.new("Folder", player)
Leaderstats.Name = "leaderstats"
for index, value in pairs(PartsFolder:GetDescendants()) do
Modules.PartHandler.new(player, value)
end
local Data
local success, errormessage = pcall(function()
Data = DataStores.StageDataStore:GetAsync(player.UserId)
end)
if success then
CurrentStage.Value = Data
end
for index, value in pairs(DataFolder:GetChildren()) do
local Cloned = value:Clone()
Cloned.Parent = Leaderstats
end
end)
Services.Players.PlayerRemoving:Connect(function(player)
local Data = player.Data.Stage.Value
DataStores.StageDataStore:SetAsync(player.UserId, Data)
end)
Services.Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
character.HumanoidRootPart.CFrame = CFrame.new(500, 50, 500)
local CurrentStage = player.Data.Stage.Value
if CurrentStage > 0 then
for index, value in pairs(PartsFolder:GetDescendants()) do
local Stage = value:GetAttribute("Stage")
if value.Name == "Checkpoint" and Stage == CurrentStage and (value:IsA("BasePart") or value:IsA("MeshPart")) then
local CharacterCFrame = character:WaitForChild("HumanoidRootPart")
character.HumanoidRootPart.CFrame = value.CFrame * CFrame.new(0,5,0) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
end
end
end
end)
end)