Math.random() doesn't bring 1, but all

I’m attempting to create a math.random() function, but unfortunately, except for bringing one person in from the table, it brings all of them. What’s the issue here? Hopefully, this will be the last time asking for assistance with this code.

	ui.Bring.MouseButton1Click:Connect(function()
		if not timeLimit then
			timeLimit = true
		local tpBlock = game.Workspace:FindFirstChild("IntTp"..val)
		local hasPlr = false
		local whitelist = {}
		for _, i in ipairs(game.Players:GetPlayers()) do
			local magnitude = (i.Character:WaitForChild("HumanoidRootPart").Position - game.Workspace.IntChecker.Position).Magnitude
			if magnitude <= 20 and i:GetRankInGroup(5301253) <= 6 then
				table.insert(whitelist, i)
				hasPlr = true
			else
				hasPlr = false
				end
			end
		if not hasPlr then
			ui.Bring.Text = "NO INTERVIEWEES"
			wait(.6)
			ui.Bring.Text = "BRING INTERVIEWEE"
		else
			local tpBlock = game.Workspace:FindFirstChild("IntTp"..val)
			local cPlr = whitelist[math.random(1, #whitelist)]
			cPlr.Character:WaitForChild("Humanoid").Jump = true
			wait(.6)
			cPlr.Character:WaitForChild("HumanoidRootPart").Position = tpBlock.Position
			ui.Bring.Text = "BRUNG "..string.upper(cPlr.Name)
			wait(.6)
			ui.Bring.Text = "BRING INTERVIEWEE"
			end
		end
		wait(1)
		timeLimit = false
	end)
end)

You missed a end at the top where you say
if not timeLimit then

I didn’t miss an end - I’m specifically talking about math.random() bringing all instead of choosing 1 person from the “whitelist” table.

I think the issue is, the for loop checks if every single player is teleportable and does accordingly

My bad just re read the code, The indenting is weird. Let me check the issue.

It seems like you forgot to end the for loop?

From what I’m seeing, it checks every player’s magnitude and rank once clicked, and if that player is under the magnitude of 20 studs and is below rank 6 in that group, then it’ll insert them. Yes, there’s an end for that loop before it gets a random player from “whitelist” table.

Did you check to ensure the white list is not empty.

What is this code inside of? You have two ends at the bottom but we only see the MouseButton1Click

I don’t think it needs checking, as once clicked, it’ll clear the table and insert specific players in.

game.Players.PlayerAdded:Connect(function(plr) end)

Instead of using the hasPlr variable, check to see if the table contains a value

Yes, but I don’t think that’d be relevant to math.random() function bringing everyone.

But it’s still a mistake within the script, because let’s say the last player didn’t meet the requirements - then hasPlr would be false so noone would be teleported.

Just curious if you were to use this instead does it fix your issue? I honestly can’t find an issue with your code. If I understand you correctly it is teleporting all valid players when it should only teleport one random person.

local Debounce = false
local IntChecker = game.Workspace:WaitForChild("IntChecker")

local function BringRandomPlayer()
	if Debounce then
		return
	end
	Debounce = true
	local AllowedPlayers = {}
	for every, player in pairs(game.Players:GetPlayers()) do
		local Character = player.Character
		if Character then
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			if HumanoidRootPart then
				local Distance = (HumanoidRootPart.Position - IntChecker.Position).Magnitude
				if Distance <= 20 and player:GetRankInGroup(5301253) <= 6 then
					table.insert(AllowedPlayers, player)
				end
			end
		end
	end
	if #AllowedPlayers > 0 then
		local tpBlock = game.Workspace:FindFirstChild("IntTp"..val)
		local RandomPlayer = AllowedPlayers[math.random(1, #AllowedPlayers)]
		local RandomPlayerCharacter = RandomPlayer.Character
		RandomPlayerCharacter:WaitForChild("Humanoid").Jump = true
		wait(0.6)
		RandomPlayerCharacter:WaitForChild("HumanoidRootPart").Position = tpBlock.Position
		ui.Bring.Text = "BRUNG " .. string.upper(RandomPlayer.Name)
		wait(0.6)
		ui.Bring.Text = "BRING INTERVIEWEE"
	else
		ui.Bring.Text = "NO INTERVIEWEES"
		wait(0.6)
		ui.Bring.Text = "BRING INTERVIEWEE"
	end
	wait(1)
	Debounce = false
end

Where are you trying to call BringRandomPlayer() from?

Inside your .MouseButton1Click if the UI is defined inside the PlayerAdded simply just add it as a Argument in the function.

Tried your script out, brings everyone in the table.

Can you post the entire script?

--[ Variables
local monitor = script.Parent
local ui = monitor.MonitorUi
local val = monitor.IntRoom.Value
--[ Setup
game.Players.PlayerAdded:Connect(function(plr)
	local doorCollision = false
	local timeLimit = false
	ui.Room.Text = "Room "..val
	ui.Door.MouseButton1Click:Connect(function()
		local door = game.Workspace:FindFirstChild("IntDr"..val)
		if not doorCollision then
			for _, i in pairs(door:GetDescendants()) do
				if i:IsA("Part") then
					ui.Door.Text = "OPEN DOOR"
					i.CanCollide = true	
					doorCollision = true
				end
			end
		elseif doorCollision then
			for _, i in pairs(door:GetDescendants()) do
				if i:IsA("Part") then
					ui.Door.Text = "CLOSE DOOR"
					i.CanCollide = false
					doorCollision = false
				end
			end
		end
	end)
	ui.Bring.MouseButton1Click:Connect(function()
		local Debounce = false
local IntChecker = game.Workspace:WaitForChild("IntChecker")
	if Debounce then
		return
	end
	Debounce = true
	local AllowedPlayers = {}
	for every, player in pairs(game.Players:GetPlayers()) do
		local Character = player.Character
		if Character then
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			if HumanoidRootPart then
				local Distance = (HumanoidRootPart.Position - IntChecker.Position).Magnitude
				if Distance <= 20 and player:GetRankInGroup(5301253) <= 6 then
					table.insert(AllowedPlayers, player)
				end
			end
		end
	end
	if #AllowedPlayers > 0 then
		local tpBlock = game.Workspace:FindFirstChild("IntTp"..val)
		local RandomPlayer = AllowedPlayers[math.random(1, #AllowedPlayers)]
		local RandomPlayerCharacter = RandomPlayer.Character
		RandomPlayerCharacter:WaitForChild("Humanoid").Jump = true
		wait(0.6)
		RandomPlayerCharacter:WaitForChild("HumanoidRootPart").Position = tpBlock.Position
		ui.Bring.Text = "BRUNG " .. string.upper(RandomPlayer.Name)
		wait(0.6)
		ui.Bring.Text = "BRING INTERVIEWEE"
	else
		ui.Bring.Text = "NO INTERVIEWEES"
		wait(0.6)
		ui.Bring.Text = "BRING INTERVIEWEE"
	end
	wait(1)
	Debounce = false
	end)
end)