Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Teleporting players to a spawnpoint. Advice?

Asked by 5 years ago

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

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 theking48989987 2147 — 5y
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 theking48989987 2147 — 5y
0
it worked for me? the8bitdude11 358 — 5y
0
Reference the Character instead, use :MoveTo() Ziffixture 6913 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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)
Ad
Log in to vote
-2
Answered by 5 years ago

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!

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 User#24403 69 — 5y
0
-1 Using WaitForChild when there are events available for the task User#5423 17 — 5y

Answer this question