Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Printing a random player's name and excluding my name not working? (Beginner)

Asked by
imaA6D 39
3 years ago

I am trying to print ONE random players name and excluding my name, I can't get it to work. Any help please?

Error: print(PlayerList[math.random(#PlayerList)]):7: invalid argument #1 to 'random' (interval is empty)

local LocalPlayer = game.Players.LocalPlayer
local Players = game.Players:GetPlayers()
local PlayerList = {Players}

table.remove(PlayerList, table.find(PlayerList, LocalPlayer))

print(PlayerList[math.random(#PlayerList)])

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Best thing to do is the following:

RandomPlayer = PlayerList[math.random(1,#Playerlist)])
--[[Now you can exclude your name by randomizing the name repeatedly until the name is NOT yours.]]
repeat 
RandomPlayer = PlayerList[math.random(1,#Playerlist)]) --Getting a new random player.
until RandomPlayer ~= --Enter your Username here.

print(RandomPlayer)

Also, i havent checked the code. If there are any issues, tell me and well fix them.

0
You need two numbers for math.random, so just do 1, #Playerlist DarkDanny04 407 — 3y
0
Sorry, i forgot.. User#34929 0 — 3y
0
It doesn't work. imaA6D 39 — 3y
0
I managed to fix your script, thank you for your time though. imaA6D 39 — 3y
Ad

Answer this question