Teleport Script Help (Solved)

Hello everybody!

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

Any help is appreciated!

-poker_man29

fromHum:PivotTo((toHum.PrimaryPart.CFrame + toHum.PrimaryPart.CFrame.LookVector * 5) * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0))

Thank you for the reply!

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.

Best regards,
poker_man29.

1 Like

Bumping as it’s been 12 hours with no response.

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

Oh crap, you’re probably right. Let me fix that and see how that works, and I’ll let you know.

Hello there,

Thank you for your reply!

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.

Regards,
poker_man29

Hello there, thank you for the reply!

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!

Regards,
poker_man29.

Hello there,

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!

Regards,
poker_man29

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.