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 :)
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!