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)
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.
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
--[ 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)