Hey all! Having some serious difficulty getting teleportation to work on spawn correctly in my game. I’ve been able to isolate it down to this flow that seems to be breaking. Anyone have any idea why?
local Players = game:GetService("Players")
local function teleport(player)
player.character:WaitForChild("HumanoidRootPart") -- Wait for the HumanoidRootPart to load
print("Teleporting!")
player.character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(100, 100, 100)) -- Teleport to position
end
local function setup(player)
player.CharacterAdded:Connect(teleport)
end
Players.PlayerAdded:Connect(setup)
I see an issue with your CFrame setting here. Don’t set the CFrame of the HumanoidRootPart only, set the CFrame of the character model. Also CharacterAdded passes in the character not the player so your code for teleport should look more like this:
local function teleport(character)
character:PivotTo(CFrame.new(Vector3.new(100, 100, 100))) -- Teleport to position
end
Check if this works. There may also be an issue where you aren’t catching all players in the PlayerAdded. I think this is still an issue but basically what happens is that the players join before you connect the PlayerAdded function which will miss some players.
So as well as connecting PlayerAdded, I would loop through the initial player list like this so you don’t miss any:
for _, player in ipairs(Players:GetPlayers()) do
setup(player)
end
yeah its probably preferred to use PivotTo but also this worked just fine, the problem here is that roblox is handling the spawn for you. So you would use task.wait() to wait for one frame after the spawn is handled and then teleport them. Make sure you’re using a server script.
you could also get rid of other scripts that are handling spawns or even get rid of SpawnLocation instances. although you can change the SpawnLocation when something happens instead of doing manual spawns.
I think the main advantage of PivotTo is that it does not require any part to be present when you call it, so there is no unnecessary yielding. Also, the script seems to work fine without task.wait(). Maybe its just my machine being different to yours, but it may be safe to remove it.
PivotTo worked perfectly for this use-case, but unfortunately it’s still not working in my non-trimmed down script but this atleast put me on the right track. Completely forgot character is passed instead of player for CharacterAdded. Will update if/when I figure it out
looks like in a blank test baseplate, it works fine without task.wait(), but with a much larger game this is actually necesarry. Adding task.wait() before teleportation seems to have fixed the issue! Does not work without this.