I'm creating a parkour type map, which starts when someone presses a button that teleports all players to parkour ... the problem is that I wanted to change the angle of the players when they are teleported. So what I did was specify what the player was and what should be changed, but there was an error and it doesn't work, could someone help me?
script:
wait(6) local player = game.Players:GetChildren() wait(0.001) player.Character.HumanoidRootPart.Orientation = Vector3.new(100,100,100)
wait(6) local player = game.Players:GetChildren() wait(0.001)
player.Character.HumanoidRootPart.Orientation = Vector3.new(100,100,100)
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() -- This will give us our table of players for i, p in pairs(plrs) do if workspace:FindFirstChild(p.Name) then -- This checks to see if the Character exists p.Character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0) -- Insert your CFrame here end end
Example of using a numeric for
loop:
local plrs = game.Players:GetPlayers() -- This will give us our table of players for i = 1, #plrs, 1 do if workspace:FindFirstChild(plrs[i].Name) then -- This checks to see if the Character exists plrs[i].Character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0) -- Insert your CFrame here 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.