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?
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:
GetPlayers() executes before any players have entered, and returns {}, an empty table
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.
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.
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