I am trying to use math.random to choose a random player.?
I need to choose a random player from a script, but since I am not good at scripting, I don’t really know how I would do it.
I have tried math.random but it gives me an error that says “Workspace.Dummy.Script:4: invalid argument #2 to ‘random’ (interval is empty)” for some reason they are taking math as something in the player.
local humanoid = script.Parent.Humanoid
local Players = game:GetService("Players")
local playerr = Players:GetChildren()
local playerRandom = math.random(1,#playerr)
print(playerRandom)
local humanoid = script.Parent.Humanoid
local Players = game:GetService("Players")
local playerr = Players:GetPlayers()
local playerRandom = math.random(1,#playerr)
local ChoosenPlayer = playerr[playerRandom]
print(ChoosenPlayer.Name)
this right here is erroring because sometime there can be only 1 player and thus the program is doing math.random(1,1) which will error as it cant choose any so try puttin an or like this
local humanoid = script.Parent.Humanoid
local Players = game:GetService("Players")
local playerr = Players:GetPlayers()
local playerRandom = math.random(1,#playerr) or 1
local ChoosenPlayer = playerr[playerRandom]
print(ChoosenPlayer.Name)
local players = game:GetService('Players')
function getTroller()
math.randomseed(tick())
local playerlist = players:GetPlayers()
if (#playerlist < 1) then return print('no player :flushed:') end
return playerlist[math.random(1, #playerlist)]
end
local players = {}
local service = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(plyr)
table.insert(players,plyr)
end)
game.Players.PlayerRemoving:Connect(function(plyr)
local removingplyr = table.find(players,plyr)
if removingplyr then
table.remove(players,removingplyr)
end
end)
local function randplayer()
local rand = math.random(1,#players)
for i,v in ipairs(service:GetPlayers()) do
if i == rand then
return v
end
end
end
wait(20)
local randplayer = randplayer()
print(randplayer.Name)