Well, here's what you're doing wrong (in brackets):
player[.Character]
If you go back to line 2, which is:
local player = game.Players:GetChildren()
Line 2 assigns a variable, named player, to the value that GetChildren() returns. GetChildren() does not return a userdata, it returns a table. You're basically trying to index something that isn't in the table.
To teleport every player to the map, you have to run a loop through the table. Here are some things to keep in mind:
GetPlayers(), another method of the Players service, is superior to GetChildren() in this situation because it returns a table that contains only players. If there was a child of the Players service that wasn't a player and you looped through the table returned by GetChildren() without checking to see if the object was a player, the loop would error out.
In this situation, you can use either a generic or numeric for loop. I will provide an example of both because both work.
Example of using a generic for loop:
local plrs = game.Players:GetPlayers()
for i, p in pairs(plrs) do
if workspace:FindFirstChild(p.Name) then
p.Character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
end
end
Example of using a numeric for loop:
local plrs = game.Players:GetPlayers()
for i = 1, #plrs, 1 do
if workspace:FindFirstChild(plrs[i].Name) then
plrs[i].Character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
end
end
Everything seems fine, but line 4 may be somewhat out of the ballpark; why use a CFrame instead of dedicated Position and Orientation?
A CFrame is both Position and Orientation. If you assign the root part's CFrame to [0, 0, 0], both the Position and Orientation of the root part will be changed to [0, 0, 0]. If you do not want both to be changed to the same 3d vector, you can use the two parameter CFrame, which takes the positional vector to assign and the directional vector to assign as well.
1 comment
sincerely, thanks man. Just to remember, the use of * For in pairs * is to name the names of some place, correct? As the workspace..it's really a good use, but thanks for the answer