I’m having some trouble with this teleport command, more specifically, getting it to teleport everyone in the server to in front of my position. It works when only teleporting a singular person, but it doesn’t work when teleporting multiple people. If I teleport myself, along with other people, it doesn’t work at all, but if I don’t teleport myself, it only teleports one person to me. Would anyone know how to fix this? Notes are included in the script to describe the script’s intentional function, if it helps.
Edit 1: I’ve edited the script portion so people can see exactly where the errors kept coming in. I believe that the error are being caused by something in the loops, too, but I didn’t get any errors in the dev console for that, though.
--fromPlr and toPlr are lists, containing player instances. basically it is supposed to teleport a fromPlr to a toPlr.
local function tP(fromPlr, toPlr)
--loop through every player in the "from" list
for i = 1, #fromPlr do
--loop through everyone in the "to" list
for i = 1, #toPlr do
--define the two player's characters
local fromHum = fromPlr[i].Character --this kept coming up with errors in the dev console
local toHum = toPlr[i].Character
--make sure the players are not sitting
fromHum.Humanoid.Sit = false
toHum.Humanoid.Sit = false
--teleports people in front of character
fromHum.PrimaryPart.CFrame = (toHum.PrimaryPart.CFrame + toHum.PrimaryPart.CFrame.LookVector * 5) * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0)
end
end
end
Although I greatly appreciate your contribution, that part of the code in particular is basically the same thing as my code, with some tweaks, and this part is also not the problem for my case. Once again, I highly enjoy your help.
Hello, I am not sure if this is the solution but it’s a common mistake I make alot. On the second loop, you should change the letter i to a different one as I think it overrides the one on the first loop.
Essentially what I did is removed the fromPlr parameter and created a getPlayers() function. So whenever you call the tp() function only specify toPlr, and remove fromPlr. Also I removed the part where it loops the toPlr list, make sure its a singular instance.
local function getPlayers() -- gets all players
local playersTable = {}
for _, players in pairs(game.Players:GetChildren()) do
if players:IsA("Player") then
table.insert(playersTable, players)
end
end
return playersTable
end
local function tP(toPlr)
local fromPlr = getPlayers()
--loop through every player in the "from" list
for i = 1, #fromPlr do
--loop through everyone in the "to" list
--define the two player's characters
local fromHum = fromPlr[i].Character --this kept coming up with errors in the dev console
local toHum = toPlr.Character
--make sure the players are not sitting
fromHum.Humanoid.Sit = false
toHum.Humanoid.Sit = false
--teleports people in front of character
fromHum.PrimaryPart.CFrame = (toHum.PrimaryPart.CFrame + toHum.PrimaryPart.CFrame.LookVector * 5) * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0)
end
end
local function teleportPlayers(fromPlr, toPlr)
-- Ensure both lists have the same number of players
if #fromPlr ~= #toPlr then
warn("Player lists are not of equal length.")
return
end
-- Loop through each player in the lists
for i = 1, #fromPlr do
local fromCharacter = fromPlr[i].Character
local toCharacter = toPlr[i].Character
-- Check if both characters exist
if fromCharacter and toCharacter and fromCharacter.PrimaryPart and toCharacter.PrimaryPart then
-- Make sure the players are not sitting
if fromCharacter:FindFirstChild("Humanoid") then
fromCharacter.Humanoid.Sit = false
end
-- Teleport in front of the target character
local targetCFrame = toCharacter.PrimaryPart.CFrame
fromCharacter:SetPrimaryPartCFrame(targetCFrame + targetCFrame.LookVector * 5 - Vector3.new(0, 3, 0))
else
warn("One of the characters is missing.")
end
end
end
I already have a getPlayers() function inside the script, for deciding what players to get in the admin script. Also, the script needs to have flexibility to be able to teleport either multiple people or singular people. I appreciate your response, though.
Unfortunately, this function is not going to work, since the function has to require flexibility to be able to teleport either singular people, or multiple people. Thank you for your suggestion though!
After testing what you suggested, I found that solution actually worked! I guess I made the same mistake as you often do. I feel kinda dumb now knowing the answer to be honest. Anyway, thanks for your help!