Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Teleporting players to a spawnpoint. Advice?

I am trying to teleport players that join the game to a certain spawnpoint, I've been looking how to do it but I can't find anything that I understand.

Here is my code:

local LobbySpawnLocations = workspace:FindFirstChild("LobbySpawnLocations")

local LobbySpawnPoints = LobbySpawnLocations:GetChildren()

local Players = game:GetService("Players")



Players.PlayerAdded:Connect(function(player)
	if player then
		player.Character.HumanoidRootPart.CFrame = game.Workspace.LobbySpawnLocations.LobbySpawn.CFrame + Vector3.new(0,5,0)
	else
		
	end
	
end)

Thanks :)

4 comments

0

use SetPrimaryPartCFrame instead of directly modifying the HRP's cframe, which only changes the cframe of the HRP and nor the rest of the character

0

Also, the if statement isn't needed as it is a player added event, also, use a character added event, as the character is loaded in after the player

0

it worked for me?

0

Reference the Character instead, use :MoveTo()

2 answers

✓ Accepted answer

Here's what seems to be happening: player.Character... assumes that the player's Character has loaded immediately after joining, and will error in your case. Wait for the character to be loaded first, then do your thing.

You could use Player.CharacterAdded:Wait() so that your script yields until the character has been loaded.

My solution usually is while not Player.Character do wait() end, which does the same thing.

Here:

local LobbySpawnLocations = workspace:FindFirstChild("LobbySpawnLocations")

local LobbySpawnPoints = LobbySpawnLocations:GetChildren()

local Players = game:GetService("Players")


Players.PlayerAdded:Connect(function(player)
	-- wait for the character to be loaded first,
	player.CharacterAdded:Wait()
	--  then do your thing
	player.Character.HumanoidRootPart.CFrame 
 = workspace.LobbySpawnLocations.LobbySpawn.CFrame * CFrame.new(0,5,0)
end)

Try this in a Server Script

local LobbySpawnLocations = workspace:WaitForChild("LobbySpawnLocations")
local LobbySpawnPoints = LobbySpawnLocations:GetChildren()

game.Players.PlayerAdded:Connect(function(player)
	local character = workspace:WaitForChild(player.Name)
	character:SetPrimaryPartCFrame(LobbySpawnLocations.LobbySpawn.CFrame + Vector3.new(0, 5, 0))
end)

The main issue was just that the player's character had not loaded before the event fired, this will wait for the character before moving them.

Since I noticed you defined "LobbySpawnPoints", I'm assuming you may have multiple spawns, if you want I can add a section for that.

Comment below with any questions or concerns!

2 comments

0

Why can't you use the Character property? It's much more reliable than indexing the workspace for the player. Imagine if someone named "Terrain" joined your game lol

0

-1 Using WaitForChild when there are events available for the task

This discussion is preserved as it was. New answers, comments, and votes are closed.