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

Workspace.teleport.buttom3.rotaçao:5: attempt to index field 'Character' (a nil value). ?

Asked by
lytew 99
4 years ago

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)

1 answer

Log in to vote
0
Answered by 4 years ago

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:

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

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

0
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 lytew 99 — 4y
Ad

Answer this question