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
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.
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.