Issue has been solved

Please ONLY help with the Teleportation issue, any replies unrelated to this will be ignored

I have a check Point system that teleports you your check point, it does work but sometimes when you first join the game, the character not fully loaded yet and you don’t get teleported until you die, any solution?

 local players = game:GetService("Players")
 local dataStoreService = game:GetService("DataStoreService")
 local saveDataStore = dataStoreService:GetDataStore("DataStore")
 CurrentCheckPoint = {}
 
 function newCharacter(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 
 	local success, thecheckpoint = pcall(function()
 		data = saveDataStore:GetAsync(plr.UserId)
 	end)
 	if success then
 		CHECKPOINTS.Value = data
 
 		if CHECKPOINTS.Value == 0 then
 			CHECKPOINTS.Value = 1
 		end
 	end

--Teleports Players to their checkPoint
 	plr.CharacterAdded:connect(function(char)
 		if CHECKPOINTS.Value ~= nil then
		local part = workspace.CheckPoints:WaitForChild(CHECKPOINTS.Value)
 			local PRIMPART = char:WaitForChild("HumanoidRootPart")
 			repeat wait(0.1) until PRIMPART ~= nil
			print("Loaded")
 			PRIMPART.CFrame = part.CFrame + Vector3.new(0,1,0)
 		end
 	end)
 end
 

 players.PlayerAdded:connect(function(plr)
 	newCharacter(plr)
 end)

The CharacterAdded event fires before you connect a function to it, so you have to run the function once for all characters already loaded.

So wait for the character to load before running the function?

You could also do that, but I would do this:

local function CharacterAdded(char)

end

for _, player in pairs(game.Players:GetChildren()) do
    if player.Character then
        coroutine.wrap(CharacterAdded)(player.Character)
    end
end

plr.CharacterAdded:Connect(CharacterAdded)

By the way, this is not very efficient. You can use the Humanoid.Died event for this.

1 Like