Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

TeleportPlayers Custom Function Does Not Seem to Work?

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

✓ Accepted answer

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)

2 comments

0

gg

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.

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 _,

1 comment

0

gg

This discussion is preserved as it was. New answers, comments, and votes are closed.