Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

random Player teleporter teleporting all players?

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

✓ Accepted answer

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! :)

2 comments

0

Thanks for helping me fix it! I owe you one ;)

0

No worries! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.