Help in teleporting character using :PivotTo()

Hello. I’m trying to teleport a user from one part to part while it’s spawned. But for some reason it won’t teleport.

This is my code:

Player.CharacterAdded:Connect(function(Character)
		
		for Index, Part in pairs(Character:GetChildren()) do
			
			if Part:IsA("BasePart") then
				
				Part.CollisionGroup = "Player"
				print("Set "..Part.Name.."'s collision group.")
				
			end
			
		end
		
		local Checkpoint = CheckpointsFolder:FindFirstChild(tostring(Level.Value)) or workspace.Spawn
		print(Checkpoint.Name)
		if not Checkpoint then warn("Checkpoint "..Checkpoint.." not found while teleporting "..Player.Name..".") return end
		
		Character:PivotTo(Checkpoint.CFrame * CFrame.new(0, 5, 0))
		
	end)

The print statements work too, but it doesn’t show any errors or anything.
image

3 Likes

Does the script print the statement, it could be the if statement stopping the teleportation.

You can use a vector3 and add it to Checkpoint CFrame rather than multiplying it.
That’s just how I would do it.

Also where is this script located? And what type of script it is.

1 Like

Yes, the script does print the checkpoint name, “3”.
I tried adding Vector3 to the CFrame of the checkpoint but it didn’t work.

It’s a server script in SSS. ____________________________

1 Like

Interesting, the script seems to work for me. Could you recheck if Checkpoint is a BasePart? If that doesn’t work you may have to wait until the HumanoidRootPart loads using :WaitForChild()

1 Like

try printing the characters name, see what it is.

tested this out, this works, im guessing it just can’t find the right checkpoint, try using position, and using :MoveTo().

tested this out and it works:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		task.wait(5)
		character:PivotTo(workspace.SpawnLocation.CFrame+Vector3.new(0,5,0))
	end)
end)
1 Like

Yes, checkpoint is a BasePart. I checked using :IsA. I don’t think :WaitForChild is necessary because it is able to set it’s collision group so it’s probably already loaded.

1 Like

I can’t wait for the teleportation as it is a checkpoint, it needs to teleport immediately.
:MoveTo didn’t work either, nor did setting the character’s and HRP’s CFrame manually.

1 Like

I just put the wait for testing so I have time to walk away in testing since I can’t replicate your environment 1:1.

Try putting a print statement at the very bottom of the script and before the :PivotTo(), because your code seems fine.

1 Like

Yup, it prints after the :PivotTo but the character still won’t teleport.

1 Like

Could you print out the CFrame of your Checkpoint and see if thaf is causing the issue?

1 Like


I don’t think this is normal is it?

1 Like

No, this is a normal CFrame. I am puzzled as this works fine with me, are you sure there isn’t any other “teleportation” function interfering with this? If not could I see a bit more of the script please.

1 Like

No there’s no other teleportation script.
Here’s the whole script:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")
local BadgeService = game:GetService("BadgeService")

PhysicsService:RegisterCollisionGroup("Player")
PhysicsService:CollisionGroupSetCollidable("Player", "Player", false)

local DataStore = DataStoreService:GetDataStore("Main")

local RemoteEvents = ReplicatedStorage.RemoteEvents
local ChatMessageEvent = RemoteEvents.ChatMessage

local CheckpointsFolder = workspace:WaitForChild("Checkpoints")

local WelcomeBadgeID = 2125710098

Players.PlayerAdded:Connect(function(Player)
	
	local AreNamesSame = Player.Name == Player.DisplayName
	
	if Player.UserId == game.CreatorId then
		
		local Message = not AreNamesSame and "The Owner has joined! Welcome, "..Player.DisplayName.." (@"..Player.Name..")!" or "The Owner has joined! Welcome, "..Player.DisplayName.."!"
		ChatMessageEvent:FireAllClients(Message, Color3.fromRGB(45, 220, 255))
		
	else
		
		local Message = not AreNamesSame and "A player has joined! Welcome, "..Player.DisplayName.." (@"..Player.Name..")!" or "A player has joined! Welcome, "..Player.DisplayName.."!"
		ChatMessageEvent:FireAllClients(Message, Color3.fromRGB(255, 255, 255))
		
	end
	
	if not BadgeService:UserHasBadgeAsync(Player.UserId, WelcomeBadgeID) then
		
		BadgeService:AwardBadge(WelcomeBadgeID)
		
	end
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Money = Instance.new("IntValue")
	Money.Name = "$$"
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	
	Level.Parent = leaderstats
	Rebirths.Parent = leaderstats
	Money.Parent = leaderstats
	
	local Data = nil
	local Success, Error = pcall(function()
		
		Data = DataStore:GetAsync(Player.UserId) or {}
		
	end)
	
	if not Success then warn("Failed to fetch "..Player.Name.."'s data.") end
	
	Money.Value = Data.Money or 0
	Rebirths.Value = Data.Rebirths or 0
	Level.Value = Data.Level or 1
	
	Player.CharacterAdded:Connect(function(Character)
		
		for Index, Part in pairs(Character:GetChildren()) do
			
			if Part:IsA("BasePart") then
				
				Part.CollisionGroup = "Player"
				print("Set "..Part.Name.."'s collision group.")
				
			end
			
		end
		
		local Checkpoint = CheckpointsFolder:FindFirstChild(tostring(Level.Value)) or workspace.Spawn
		print(Checkpoint.Name)
		print(Checkpoint.CFrame)
		
		if not Checkpoint then warn("Checkpoint "..Checkpoint.." not found while teleporting "..Player.Name..".") return end
		
		local HumanoidRootPart = Character.PrimaryPart
		
		Character:PivotTo(Checkpoint.CFrame + Vector3.new(0, 5, 0))
		print("Teleported.")
		
	end)
	
end)

Players.PlayerRemoving:Connect(function(Player)
	
	pcall(function()
		
		local leaderstats = Player.leaderstats
		
		local Money = leaderstats["$$"].Value
		local Rebirths = leaderstats.Rebirths.Value
		local Level = leaderstats.Level.Value
		
		local Data = {
			["Money"] = Money,
			["Rebirths"] = Rebirths,
			["Level"] = Level
		}
		
		DataStore:SetAsync(Player.UserId, Data)
		
	end)
	
end)

game:BindToClose(function() task.wait(4) end)
1 Like

You can’t add up CFrames, you have to multiply.

And there’s just no point in converting it into a CFrame. The value is still equal.

1 Like

Yeah I don’t know what I was waffling about there

3 Likes

Hi there!

Have you tried teleporting the HumanoidRootPart instead of the whole model?

Yes, that didn’t work either… I’m still trying to figure this out.

I’ve played around with this script a little, and I can’t seem to find why it doesn’t work for you. It works for me and some other people on this thread. Maybe you could try to locate any other scripts that could be affecting the player at all, you could try to put the server script inside of a new studio place, and see if it’ll work for you. If it does it means there is something you messed up along the way originally. You could also try to delete the checkpoints and then replace them and see if that fixes the issue. Make sure to double check all the names and everything. Again not sure why this isn’t working for you.

Here’s a clip of everything working on my end.

Turns out I was teleporting before the character spawned at 0, 0, 0 maybe.
I added a wait statement before the teleport statement and it worked
.

1 Like