Not teleporting to correct checkpoint

Hey everyone!

It doesn’t seem to teleport me to the correct checkpoint. For instance if I was on Red, and I left, the game is now suddenly teleporting me to Blue? And if I rejoin again I’m now at Orange. I have no idea what is happening or why. Here is my code:

local function teleportPlayerToCheckpoint(plr, char)
    local teamName = plr.Team and plr.Team.Name
    if teamName then
        local checkpoints = workspace:FindFirstChild("Checkpoints")
        if checkpoints then
            local checkpoint = checkpoints:FindFirstChild(teamName)
            if checkpoint then
                local hrp = char:FindFirstChild("HumanoidRootPart")
                if hrp then
                    hrp.CFrame = checkpoint.CFrame + Vector3.new(0, 2, 0) -- Teleport 2 studs above the checkpoint
                    print("Teleported player " .. plr.Name .. " to checkpoint: " .. checkpoint.Name)
                else
                    warn("HumanoidRootPart not found for " .. plr.Name)
                end
            else
                warn("Checkpoint for " .. teamName .. " not found.")
            end
        else
            warn("Checkpoints folder doesn't exist.")
        end
    else
        warn(plr.Name .. " is not assigned to a team.")
    end
end
--team loading
	
	local savedTeamName
	local teamSuccess, teamErr = pcall(function()
		savedTeamName = teamData:GetAsync(plr.UserId .. "-team")
	end)

	if teamSuccess and savedTeamName then
		local savedTeam = game.Teams:FindFirstChild(savedTeamName)
		if savedTeam then
			plr.Team = savedTeam
			print("Restored team for player " .. plr.Name .. ": " .. savedTeamName)
		else
			plr.Team = game.Teams.Lobby
			print("Saved team not found, assigning player to Lobby.")
		end
	else
		plr.Team = game.Teams.Lobby
		print("No saved team found, assigning player to Lobby.")
	end

	plr:GetPropertyChangedSignal("Team"):Connect(function()
		if plr.Team then
			savePlayerTeam(plr)
		end
	end)

	plr.CharacterAdded:Connect(function(char)
		teleportPlayerToCheckpoint(plr, char)
	end)
1 Like

Can you show us the part where you switch the teams? Also show us the savePlayerTeam function

1 Like

I tried this and it works perfectly fine, maybe check your checkpoint properties?

local teamData = game:GetService("DataStoreService"):GetDataStore("TeamData")

local function teleportPlayerToCheckpoint(plr, char)
	local teamName = plr.Team and plr.Team.Name
	if teamName then
		local checkpoints = workspace:FindFirstChild("Checkpoints")
		if checkpoints then
			local checkpoint = checkpoints:FindFirstChild(teamName)
			if checkpoint then
				local hrp = char:FindFirstChild("HumanoidRootPart")
				if hrp then
					hrp.CFrame = checkpoint.CFrame + Vector3.new(0, 2, 0) -- Teleport 2 studs above the checkpoint
					print("Teleported player " .. plr.Name .. " to checkpoint: " .. checkpoint.Name)
				else
					warn("HumanoidRootPart not found for " .. plr.Name)
				end
			else
				warn("Checkpoint for " .. teamName .. " not found.")
			end
		else
			warn("Checkpoints folder doesn't exist.")
		end
	else
		warn(plr.Name .. " is not assigned to a team.")
	end
end

local function savePlayerTeam(plr: Player)
	local teamSuccess, teamErr = pcall(function()
		teamData:SetAsync(plr.UserId .. "-team", plr.Team.Name)
	end)
end


game.Players.PlayerAdded:Connect(function(plr)
	
	--team loading

	local savedTeamName
	local teamSuccess, teamErr = pcall(function()
		savedTeamName = teamData:GetAsync(plr.UserId .. "-team")
	end)

	if teamSuccess and savedTeamName then
		local savedTeam = game.Teams:FindFirstChild(savedTeamName)
		if savedTeam then
			plr.Team = savedTeam
			print("Restored team for player " .. plr.Name .. ": " .. savedTeamName)
		else
			plr.Team = game.Teams.Lobby
			print("Saved team not found, assigning player to Lobby.")
		end
	else
		plr.Team = game.Teams.Lobby
		print("No saved team found, assigning player to Lobby.")
	end

	plr:GetPropertyChangedSignal("Team"):Connect(function()
		if plr.Team then
			savePlayerTeam(plr)
		end
	end)
	
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	teleportPlayerToCheckpoint(plr, char)
	
	plr.CharacterAdded:Connect(function(char)
		teleportPlayerToCheckpoint(plr, char)
	end)
end)
2 Likes

All my checkpoints work and are set to default properties. There is nothing going on.

I fixed it by moving the teleportPlayer function earlier in my code. Thanks everyone for helping!!

2 Likes

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