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

TeleportPlayers Custom Function Does Not Seem to Work?

Asked by 6 years ago
Edited 6 years ago

So I have this code that theoretically teleports the players in the lobby into the block part on the race track when the race cars are created and the race is ready to start.

local lobbySpawn = game.Workspace.LobbySpawn
local trackSpawn = game.Workspace.TrackSpawn

function teleportPlayers(target)
    for _, players in pairs(game.Players:GetChildren()) do
        local character = player.Character
        local torso = character.Torso
        torso.CFrame = target.CFrame
    end
end

teleportPlayers(trackSpawn)

I get this error:

attempt to index global 'player' (a nil value)

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

Your reference to the current iteration is players. You used player.

Change it to player and you should be good.

local lobbySpawn = workspace.LobbySpawn
local trackSpawn = workspace.TrackSpawn

function teleportPlayers(target)
    for _, player in pairs(game.Players:GetChildren()) do --'player'
        local character = player.Character
        local root = character:WaitForChild("HumanoidRootPart")
        root.CFrame = target.CFrame
    end
end

teleportPlayers(trackSpawn)
0
gg DanzLua 2879 — 6y
0
The problem is that. Also I have been testing with an R15 character, how do I convert him into to an R6 character in script. PrimeAlphinex 36 — 6y
Ad
Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago

The reason why it is erroring is because in the for loop you made the children's key variable to be named 'players' and on line 6 you are looking for 'player' just remove the 's' in 'players' on line 5 after _,

0
gg Goulstem 8144 — 6y

Answer this question