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

Why is my teleport script not working?

So I wanted to make a script so when there are more then 0 players (I will later make it higher then 0) the game will start and everyone will be teleported to this location:

local GameRunning = false

if game.Players.NumPlayers > 0 and not GameRunning then
	GameRunning = true
	game.Players:GetPlayers().Character.Torso.CFrame = game.Workspace.LivingRoom.CFrame
else
	GameRunning = false
end

2 answers

✓ Accepted answer
local GameRunning = false

while wait(1) do
	if game.Players.NumPlayers > 0 and (not GameRunning) then
		GameRunning = true
		for _,Player in pairs(game.Players:GetChildren()) do
			Player.Character.Torso.CFrame = game.Workspace.LivingRoom.CFrame
		end
	else
		GameRunning = false
	end
end

This just makes it so it is constantly checking to see if there are more than 0 players, and I've updated the teleport part as well.

I'd also recommend adding another debounce as well so that it doesn't constantly teleport the players.

I believe that tables don't have Characters.

Let's see here.

You are using GetChildren() incorrectly, if you read the article I provided you with, it says

Returns an array of the object's children.

Now, what is that?

An array is basically a Table. You are trying to get a Character of a Table!

So that is what's wrong!

Now, if you dig deeper into the article, you'll see an example of how to use GetChildren().

for index, child in pairs(workspace:GetChildren()) do
    print(index, child.Name)
end

That is called a Generic For Loop, which loops through tables, like GetChildren().

So by combining the knowledge we got with this answer, we can now properly use GetChildren!

local GameRunning = false

if game.Players.NumPlayers > 0 and not GameRunning then
	GameRunning = true
	for i,Player in pairs(game.Players:GetPlayers()) do --Generic For Loop
		Player.Character.Torso.CFrame = game.Workspace.LivingRoom.CFrame
	end
else
	GameRunning = false
end

Now, we fixed this problem, but there's another one, you're only checking once!

Say you wanted a game to start if there were more than 1 person, then you wrote this

if game.Players.NumPlayers > 1 then
	--blah blah
end

That would check, but it'll only check once. The number of players to start is supposed to be 2+, but if I join, the script will check once, and it'll finish, so that won't work!

so what we can do is hook it up to the PlayerAdded event!

game.Players.PlayerAdded:connect(function(plr)
	if game.Players.NumPlayers > 1 then
		--blah blah
	end
end)

And there you go! If you use my scripts, in the future, you'll see some problems, but that is for you to sort out!

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