Invalid argument random interval is empty

Hello Dev Form member, I have coded a system giving to players in a hazar order a role, only when the script arrives at the last player there is an error and the player just don’t get any role.
The error : ServerScriptService.GameSystem.RoleGiver:43: invalid argument #1 to 'random' (interval is empty)

The script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Attributes = require(ReplicatedStorage.Modules.Attributes)

local SVPlayers = ServerStorage.Players

local ListPlayers = {}

function setupPlayersList()
	for _, plr in pairs(Players:GetPlayers()) do
		table.insert(ListPlayers, plr.UserId)

function RoleGiver:normal()
	local PlayerInServer = #Players:GetPlayers()
	local Role = getRoles()
	while true do
		local RandomNum = math.random(#ListPlayers) -- Error here
		local RandomPlr = ListPlayers[RandomNum]
		if #ListPlayers == 0 then
			print("RoleGiver done.")
		if #Role.Werewolf < math.floor(PlayerInServer/4) then
			-- Stuff :)
		elseif true then
			-- Stuff :)
		ListPlayers[RandomNum] = nil -- Remove plr form the list

Could you highlight line 43 please?

Calling math.random with only 1 argument n implies an interval of [1, n], so this means the table is empty. I think it is a better idea to guard the function so it ends prematurely if there are no players:

if #ListPlayers < 1 then

Indeed that fixes the error, but not the all problem. There is still one player out of the 4 who did not receive their role

This is strange because its not supposed to return nil
I call the function setupPlayersList() so the table should have the 4e player too

If you want help with that error, you need to give us the part of the script with that error, and anything related.

You already have the code, the problem comes from the fact that for the last player RandomPlr becomes nil instead of the UserId of the player which is logically not normal because of this function which is called just before

What is line 30 then? Because this is supposedly line 43 here
ListPlayers[RandomNum] = nil -- Remove plr form the list
but the code you gave doesn’t have anywhere near that amount of lines, suggesting that what you posted was altered from what it originally was. Which one is line 30 then?

This is the line 30 (in a function plrId is the nil value that supposed to be the 4e plr UserId)

local ServerStorage = game:GetService("ServerStorage")
local SVPlayers = ServerStorage.Players
   function getPlayerSVData(plrId) -- Return SV Attribute data storage
	local Obj = SVPlayers:FindFirstChild(plrId) -- error here because plrId is nil
	if Obj then
		return Obj
		return nil

The function have nil error because the id plrId provided for the 4th player is also nil and cannot be found by FindFirstChild.

There is how the function is called
local PlrData = getPlayerSVData(RandomPlr)
RandomPlr should be the 4th player userid but when i print it is nil

Where are you calling that function? I think that’s line 60?

I found the problem, insead of using

i use table.remove(ListPlayers,RandomNum) and now its working