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

Why won't this teleport players to a specific brick?

Asked by 5 years ago

I'm experimenting with in pairs functions, and I'm trying to get all of the players to teleport to a specific location. Can anybody tell me what I am doing wrong here?

local players = game.Players:GetPlayers()
local repstor = game:GetService("ReplicatedStorage")
local gun = repstor.Revolver

for i,v in pairs(players) do
    local char = v.Character
    wait(3)
    char:Moveto(Vector3.new(-45.9, 0.5, 100.2))
end

The output doesn't show any errors, so what could it be?

0
There's probably no players so :GetPlayers() returned an empty array User#24403 69 — 5y
0
What do you mean? Do I need to start a server inside studio? I was going into test mode by the way. MustangHeart 67 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

It seems like your code doesn't run as you expect it to because it executes before any players have joined the server. What happens is this:

  1. GetPlayers() executes before any players have entered, and returns {}, an empty table

  2. for i,v in pairs(players) sees the empty table and runs 0 times

There are no errors because as far as the script's concerned, it didn't run into any.

Remember that server scripts can run before any players have connected to the server, so it's necessary to check for any players before running any code that involves players.

Here's the code:

-- wait for players first
local PlayersService = game:GetService("Players") -- get Players Service
if #PlayersService:GetPlayers() == 0 then -- check if there are no players
    PlayersService.PlayerAdded:Wait() -- wait for PlayerAdded
end
-- ^ this will prevent your code from running until there is at least 1 player in game


local players = PlayersService:GetPlayers() -- this will now return a table with at least 1 player
local repstor = game:GetService("ReplicatedStorage")
local gun = repstor.Revolver

for i,v in pairs(players) do
    -- check if the player's character has already loaded before executing any code
    while v.Character == nil do wait() end 
    local char = v.Character
    wait(3)
    char:MoveTo(Vector3.new(-45.9, 0.5, 100.2))
    -- Moveto changed to MoveTo(), mind your cases!
end

Also, you could just set the CFrame of the character's HumanoidRootPart to teleport them, which I'd personally recommend, like so:

for i,v in pairs(players) do
    -- check if the player's character has already loaded before executing any code
    while v.Character == nil do wait() end 
    local HRP = v.Character:WaitForChild("HumanoidRootPart") -- wait and get the root part
    wait(3)
    HRP.CFrame = CFrame.new(45.9, 0.5, 100.2) -- teleport the root part to this position
end

In addition, since you specified a certain part to teleport to, you could do this:

HRP.CFrame = PartToTeleportTo.CFrame 

This will teleport the players to the center of the part, instead of having to specify the position every time. You could do additional stuff to add offsets so the players wouldn't be inside the part.

Ad
Log in to vote
0
Answered by 5 years ago

I'm kinda in a rush so I won't answer this fully, but try doing this.

for _, player in pairs(game.Players:GetPlayers()) do
end

Your way isn't working prob. because your checking for all players before they joined.

0
What does the _, player mean? Like what do they designate? MustangHeart 67 — 5y
0
Also unless I'm doing something wrong, this didn't work. MustangHeart 67 — 5y
Log in to vote
0
Answered by 5 years ago

If you are just trying to teleport the players, you could easily set the HumanoidRootPart position using CFrame. It is what I use for all of my teleportation scripts.

It is also a matter of the players do not exist yet. The script is checking for the players before a player is even added. So I would try putting a wait before that.

This should to the trick. I tested it in a game of my own. Please comment back if it does not work in your scenario.

wait(5)

local players = game.Players:GetPlayers()
local repstor = game:GetService("ReplicatedStorage")
local gun = repstor.Revolver

for i,v in pairs(players) do
    local char = v.Character.HumanoidRootPart
    wait(3)
    char.CFrame = CFrame.new(-45.9, 0.5, 100.2)
end

Answer this question