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

How do I teleport a random player from a specific team?

I am currently making an automated obstacle course game and came across a problem. I tried scouring for the solution but couldn't find one on google so I came here.

My problem is that, I want a single random player to be picked from a team and teleported onto a block automatically. This is the code I'm using.

local status = game.ReplicatedStorage:WaitForChild("Status")
playerpicked = 0
intdone = false



status.Value = "Loading..."
wait(2)
while intdone == false do
	t = 10
	repeat
		t = t-1
		status.Value = "Intermission.. "..t
		wait(1)
	until t == 0
	status.Value = 'Starting..'
	intdone = true
	wait(2)
end

if intdone == true then
	for i,v in pairs(game.Teams["In Tourney"]:GetPlayers()) do
      --Having trouble figuring out what to do in here.
		
	end
end

If you need further detail of the question or didn't understand something I was asking, please tell me and I will rephrase the question.

10 comments

0

u mean ur not using this site from google?

0

also what is this for

0

Haha, I just meant I couldn't find solutions that were already solved and or tutorials on youtube, so I had to ask one myself as it is a pretty specific question.

0

It's for a game I'm making which is a very hard obstacle course based off a real gameshow.

0

wowowo

0

also i believed it had been answered

0

GetPlayers returns an array which you can then use math.random with the min value and max value to create a random number that is within the number of players. Then just index the table.

0

aye

0

Also you do not want to be changing the time using a value in rep storage just send the player a timer to so they can count down.

0

What exactly do you mean by Indexing the table?

1 answer

If you check the amount of players on the Team and use math.random() you can generate a random player by iterating through the :GetPlayers() table. Doing this with a loop, you also need to make sure that the amount of players on the team is not 0 (or else there is no character to move)

A for-loop has a predefined ability to continue its iteration until it reaches a goal value so you can replace the repeat loop with this.

It is also preferred to use :GetService() for Teams and ReplicatedStorage

REVISED SERVER SCRIPT

local status = game:GetService("ReplicatedStorage"):WaitForChild("Status")
status.Value = "Loading..."
local teleblock = workspace.Part -- Part Player is Teleporting To

wait(2)

while true do
	for t = 10, 0, -1 do
		status.Value = ("Intermission.. "..t)
		wait(1)
	end
	status.Value = "Starting.."
	wait(2)
	local team = game:GetService("Teams"):WaitForChild("In Tourney"):GetPlayers()
	if #team ~= 0 then
		local playerpicked = math.random(1, #team)
		for i, v in pairs(team) do
			if i == playerpicked then
				v.Character:SetPrimaryPartCFrame(teleblock.CFrame + Vector3.new(0, 4, 0))
			end
		end
		wait(60) -- Time to Complete Round
	end
end

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