Random Selector printing nil

Hello!
I’m using an open source module by @ImageLabel to make a random selector.
Post: How do i design a role-chance system, similar to Murder Mystery 2? - #5 by ImageLabel

It’s not working!
Here is my script:

local function SelectDoge()
	local selected = nil;
	
	local TotalWeight = 0;
	local Weights = {};
	
	for index, player in pairs(PlayingPlayers) do
		
		Weights[index] = 10;
		
		--if MarketplaceService:UserOwnsGamePassAsync(player.UserId, MultProduct) then
			--multiplier of 10 for every player that owns the relevant gamepass
			--Weights[index] = Weights[index] * 10
		--end
	
		TotalWeight = TotalWeight + Weights[index]
		
		Events.DogeChance:FireClient(player,TotalWeight)
	end
	
	
	local ticket = Random.new():NextInteger(1, TotalWeight);
	
	for index = 1, TotalWeight do
		
		
		ticket = ticket - Weights[index]
		if ticket <= 0 then
			
			selected = PlayingPlayers[index]
			break
		end
	end
	print(selected)
	return selected;
end

When I do, print(selected), it prints Nil. It should select a random person from the playing players!
Why is it printing nil?

Your testing with more than one player, correct?

No, just myself…

Try testing with more than one player

You know what the word nil means right? It means nothing, when you are printing the variable, it prints nil because it isn’t anything. Try moving it into the loop and tell me the output.

The reason has to be that your selected variable is nil or nothing.

I know what nil means, but why isn’t it selecting a random player and printing them?

Try putting that print statement into the loop.

Which? there’s 2…

I still got the same error…

Alrighty, let me review your code.

Might have found the error in your code.
selected = PlayingPlayers[index] is the error.
What data type is PlayingPlayers?

I’m very sorry, but I found my issue, after testing the same thing on a baseplate.

This is my code:

		for _, Player in pairs(game.Players:GetPlayers()) do
			spawn(function()
				table.insert(PlayingPlayers, Player)
			end)
		end
		
		wait()
		
		--Music(true,false,'LobbyMusic')
		
		TopText.Value = 'GameTimeText'
		
		local Map = ChosenMap:Clone()
		Map.Parent = workspace
		
		wait(.3)
		
		local Doge = SelectDoge()

I fixed it, by adding those waits. I’m guessing that the players weren’t properly inserted, so adding that fixed my issue.
Thanks!

@jumbopushpop112

You’re welcome! Glad I was able to help. :slight_smile: