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

How would I grab two randomly selected players?

Here's my code, I'm not sure if I did the plr1 and plr2 locals right, if I did how would I teleport them to to spawn1 and spawn2?

local message = Instance.new("Message")
local gamemessage = Instance.new("Message")
local localplayer = game.Players.LocalPlayer
local spawn1 = game.Workspace.Spawn1
local spawn2 = game.Workspace.Spawn2
while true do
	wait(5)
	local players = game.Players:GetPlayers()
	for i = 1, #players do
		message.Text = "2 or more players needed to start!"
		message.Parent = workspace
	end
	for i = 2, #players do
		message:Destroy()
	end
	local plr1 = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
	local plr2 = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
end

1 answer

✓ Accepted answer

Yes, you are selecting the players correctly.

To teleport them simply set their Torsos' CFrames to the corresponding spawn's CFrame.

This should be a server script in Workspace or ServerScriptStorage.

Tip: Use variables when you find yourself repeating code

local message = Instance.new("Message")
local spawn1 = workspace.Spawn1
local spawn2 = workspace.Spawn2

while wait(5) do
    local players = game.Players:GetPlayers()
	if #players < 2 then		--Check the amount of players
    	local plr = players[1];	--If it's less than 2, send out a message.	
		local m = message:Clone()
        m.Text = "2 or more players needed to start!"
    	m.Parent = plr.PlayerGui
	else						--If it's over 2, select players
		--table.remove for preventing plr1 being same as plr2
   		local plr1 = table.remove(players,math.random(1,#players))
    	local plr2 = table.remove(players,math.random(1,#players))
		--Teleport them
		plr1.Character.Torso.CFrame = spawn1.CFrame * CFrame.new(0,3,0)
		plr2.Character.Torso.CFrame = spawn2.CFrame * CFrame.new(0,3,0)
	end
end

1 comment

0

Thanks, I understand now!

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