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

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

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

✓ Accepted answer

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.

4 comments

0

You need two numbers for math.random, so just do 1, #Playerlist

0

Sorry, i forgot..

0

It doesn't work.

0

I managed to fix your script, thank you for your time though.

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