Finding character in server script

I am getting a handle on how I should approach player teleportation thanks to the people who helped me in the past forum. I changed a little things up in the script though. I need help finding character. Output says attempt to index nil with HumanoidRootPart

local player = game.Players:GetChildren()
local players = {player}
local part = game.Workspace.Part

for i, v in pairs (players) do
local player = v
v.Character.HumanoidRootPart.Position = part.Position
end

local players = {}
local part = workspace.Part

for i,v in pairs(game.Players:GetChildren())do
    table.insert(players, v)
end

for i,v in pairs(players)do
    v.Character:WaitForChild('HumanoidRootPart').Position = part.Position
end

Or even just;

for i,v in pairs(game.Players:GetChildren())do
    v.Character:WaitForChild('HumanoidRootPart').Position = part.Position
end
3 Likes

Do you recommend putting teleportation scripts in server or local?

If you are teleporting one person, it can be done locally, however if you intend on teleporting multiple people, it should be done on the server.

Thank you for the info! Is workspace the place to put it or should I put it somehwere in server stroage?

I reccomend putting Server Scripts in either;

  • Workspace
  • ServerScriptService

I reccomend putting Local Scripts in either;

  • StarterGui
  • StarterPack

Thank you for the information!

I’d like to take a moment to disagree with this statement, as some of the best places are the StarterGui, StarterCharacterScripts, or StarterPlayerScripts. Having your scripts in StarterPack is a bad choice, or even just a singular LocalScript in StarterGui (which doesn’t have a ScreenGui parent).
Otherwise, ServerScriptService should be where most of your scripts are, unless you need to have scripts in the workspace.

[OP], the way you are handling teleportation of the character isn’t exactly the best, since this could cause replication bugs or stuff like that. Instead, try this:

local Players = game:GetService("Players")
local Part = workspace:WaitForChild("Part") -- making sure it's in the workspace incase of errors

for _, Player in pairs(Players:GetPlayers()) do
    local Character = Player.Character
    if Character and Character.PrimaryPart then -- checks if the character exists, then checks if the primary part exists too
        Character:SetPrimaryPartCFrame(Part.CFrame)
    end
end

This way, you’re able to successfully teleport players that have a character with minimal to no errors. Hope this works for you.

cc @RinxfulROBLOX

Thank you it worked! Although you gave me the direct answer, to cover that, I guess I will try to expand on it and use it for my random map script.

Can you explain the script more in depth? I am trying to understand it fully

What the script does is grab the Players service which holds every player that is currently in your game. It then calls workspace:WaitForChild("Part"), which pauses the script until the ‘Part’ is in workspace.

It does a for loop in the Players service to grab each player (Players:GetPlayers()) that is ingame, then creates a local variable that holds the player’s character. The if statement is to check if the Character exists, and if that does exist, the and statement is checked to see if Character.PrimaryPart exists (the PrimaryPart is apart of every model and should be always set for Characters). The if statement is the same as doing if Character ~= nil and Character.PrimaryPart ~= nil, but they aren’t exactly required unless you’re looking for exact statements.

It then uses Character:SetPrimaryPartCFrame(), which is like how it sets the position to the part, except you’re using CFrames, also known as CoordinateFrames to set the position of the character.

1 Like

I tried to find character in server script so thank you