Character won't spawn on checkpoint

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)

Any help is appreciated.

2 Likes

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.

I solved this by using player.CharacterAppearanceLoaded:Connect()
instead.

Use this instead;

    Character or Player.UpperTorso.CFrame = workspace["YourCheckPoint"].CFrame * CFrame.new(0,5,0) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))

Actually, I deleted the spawn location and still didn’t work.

Didn’t work…

even by deleting the spawn location, roblox characters still have a “default” spawn position at around Vector3 (0,0,0)

Do you want the character to be sent there as soon as they spawn or?

Yes. When the player spawns, he will be sent to a checkpoint. If his stage is 0, then he is sent to a custom spawn location I’ll make.

Should I use this instead of CharacterAdded?

Yes, you should use this instead of characteradded.

Still.

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)

Just for anyone having the same problem, I have finally fixed the script by myself. You need to wait for the humanoid to load first.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.