Issue has been soved

I have a y check point script that teleports you to your check point, it currently works but there’s one issue

whenever the game first loads, sometimes it does not teleports you to your checkpoint, here’s the script

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("CheckPoints")


local function savePlrData(plr)
	local success,err = pcall(function()
		
		local saveData = {}
		for _,stat in pairs(plr.leaderstats:GetChildren()) do
		saveData[stat.Name] = stat.Value
	end
		saveDataStore:SetAsync(plr.UserId,saveData)
end)
	if not success then return err end
end

players.PlayerAdded:connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local CHECKPOINTS = Instance.new("IntValue")
	CHECKPOINTS.Name = "CheckPoint"
	CHECKPOINTS.Parent = stats
	CHECKPOINTS.Value = 1
	local data = saveDataStore:GetAsync(plr.UserId)
	
	if data then
		print(data.CHECKPOINTS)
		for _,stat in pairs(stats:GetChildren()) do
			stat.Value = data[stat.Name]
		end
	else
			print(plr.Name.. " Has No Data")
	end
	
	plr.CharacterAdded:connect(function(char)
		if CHECKPOINTS.Value ~= nil then
			local part = workspace.CheckPoints:WaitForChild(CHECKPOINTS.Value)
			repeat wait() until char.PrimaryPart ~= nil
			if char.PrimaryPart ~= nil then
				char.PrimaryPart.CFrame = part.CFrame + Vector3.new(0,1,0)
			end
	
		end
		
			
		
	end)
		

	
	
end)

players.PlayerRemoving:connect(function(plr)
	local err = savePlrData(plr)
	if err then print(err) end
end)

game:BindToClose(function()
	for _,plr in pairs(players:GetPlayers()) do
		local err = savePlrData(plr)
	if err then print(err) end
	end
	wait(2)
end)
1 Like

Yeah, this is due to a long-standing bug in the built-in logic for spawning characters. Basically, setting the CFrame actually does work, but it happens before the built-in spawning logic runs, which in turn ends up overwriting the changes you made. You can work around this by waiting for the built-in logic to finish whatever it’s doing. Here’s the code I used to make my custom spawning l logic work:

        player.Character.PrimaryPart.CFrame = spawnCFrame
		player.Character.PrimaryPart:GetPropertyChangedSignal("CFrame"):Wait() --Wait for built-in spawning system to do its thing
		player.Character.PrimaryPart:GetPropertyChangedSignal("CFrame"):Wait() --Wait for built-in spawning system to do its thing
		player.Character.PrimaryPart.CFrame = spawnCFrame

I haven’t observed it failing yet, but since I don’t actually know the details of the built-in logic I can’t prove that it works every time. I’ve found that waiting for 1 change isn’t always enough, and waiting for 2 changes never causes it to wait forever ¯\(ツ)

Hope this helps

3 Likes

i am gonna give it a try, gonna do multiple testing on it

1 Like

it’s Still sometimes failing to teleport to the checkpoint whenever the game first loads

you should probably use a pcall function for receiving/saving data.

Please.

local success, result = pcall(function()
    return saveDataStore:GetAsync(plr.UserId)
end)
if success then
    — use result as to load your data.

local function savePlrData(plr)
local success,err = pcall(function()

	local saveData = {}
	for _,stat in pairs(plr.leaderstats:GetChildren()) do
	saveData[stat.Name] = stat.Value
end
	saveDataStore:SetAsync(plr.UserId,saveData)

end)
if not success then return err end
end

[/quote]

Please indent your function correctly. It’s so messy.

You don’t have to use pcall function to protect the for loop from getting an error.
2 Likes

thanks for the tip!

can you also help me fix the teleportation issue? sometimes the player doesn’t teleport to the checkPoint when they first join the game

plr.CharacterAdded:connect(function(char)
if CHECKPOINTS.Value ~= nil then
local part = workspace.CheckPoints:WaitForChild(CHECKPOINTS.Value)
repeat wait() until char.PrimaryPart ~= nil
if char.PrimaryPart ~= nil then
char.PrimaryPart.CFrame = part.CFrame + Vector3.new(0,1,0)
end
end
end)

Try making a local function to teleport the player to the checkpoint.

local function TeleportToCheckpoint(chr, checkpoint)
	if checkpoint ~= nil then
			local part = workspace.CheckPoints:WaitForChild(checkpoint)
			repeat wait() until chr.PrimaryPart ~= nil
			if chr.PrimaryPart ~= nil then
				chr.PrimaryPart.CFrame = part.CFrame + Vector3.new(0,1,0)
			end
	
		end
end

plr.CharacterAdded:Connect(TeleportToCheckpoint, CHECKPOINTS.Value) -- Add this in the Player Added event function.