Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

random Player teleporter teleporting all players?

Asked by 8 days ago

Hello, at one point in my game I need 1 player to be teleported to a specific position in the game. However, instead of one player, all players are being teleported and I am not sure why. The script is located in ServerScriptService. I have done a lot of research and cannot find an answer!.

Code:

while true do
    wait(0.1)
    if #game.Players:GetChildren() > 2 then
        wait(0.1)
        function teleAll(x, y, z)
            local pos = Vector3.new(x, y, z)
            for _, plr in pairs(game:GetService("Players"):GetPlayers(RandomPlayer())) do
                if plr.Character then
                    plr.Character:MoveTo(pos)
                end
            end
        end
        function RandomPlayer()
            local holdPlayers = {}

            for _,v in pairs(game.Players:GetPlayers()) do
                table.insert(holdPlayers, v.Name)
            end

            local randomNum = math.random(1, #holdPlayers)
            local randomPlayerName = holdPlayers[randomNum]
            local randomPlayer = game.Players:FindFirstChild(randomPlayerName)
            if randomPlayer then
                return randomPlayer
            else
                return
            end
        end
        while true do
            wait(0.1)
            if game.Workspace.CaveTP.Value==1 then
                wait(2)
                teleAll(-1329.166, -102.418, -20.102)
                script:Destroy()
            end
        end
    end
end

1 answer

Log in to vote
0
Answered by
TGazza 210 Moderation Voter
8 days ago

The problem is on line 7

for _, plr in pairs(game:GetService("Players"):GetPlayers(RandomPlayer())) do

this wont work and will return All players not just the selected player. I've re-written the code so it will work with only 1 player and teleports them to your cave wp

CODE:

local StopLoop = false
while true do
    wait(0.1)
    if #game.Players:GetChildren() > 2 then
        wait(0.1)
        function RandomPlayer()
            local holdPlayers = {}

            for _,v in pairs(game.Players:GetPlayers()) do
                table.insert(holdPlayers, v.Name)
            end

            local randomNum = math.random(1, #holdPlayers)
            local randomPlayerName = holdPlayers[randomNum]
            local randomPlayer = game.Players:FindFirstChild(randomPlayerName)
            if randomPlayer then
                return randomPlayer
            else
                return
            end
        end
        function teleAll(x, y, z)
            local pos = Vector3.new(x, y, z)
            local SelectedPlayer = RandomPlayer() 
            --for _, plr in pairs(game:GetService("Players"):GetPlayers(RandomPlayer() )) do
            if(SelectedPlayer ~= nil) then
                if SelectedPlayer.Character then
                    print("Moving Player ",SelectedPlayer.Name," to cave!..")
                    SelectedPlayer.Character:MoveTo(pos)
                    print("Done Moving Player ",SelectedPlayer.Name," to cave!!!")
                end
            end
        end

        while true do
            wait(0.1)
            if game.Workspace.CaveTP.Value==1 then
                wait(2)
                teleAll(-1329.166, -102.418, -20.102)
                --script:Destroy()
                StopLoop = true
                break
            end
        end
    end
    if(StopLoop == true) then
        break
    end
end
print("Broken Looppyy!!!")

if you want the script to be removed just comment out/delete the breaks and the StopLoop varables

Hope this helps! :)

0
Thanks for helping me fix it! I owe you one ;) mistermaster06 3 — 7d
0
No worries! :) TGazza 210 — 5d
Ad

Answer this question