Random Selector printing nil

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?

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
		TotalWeight = TotalWeight + Weights[index]
	local ticket = Random.new():NextInteger(1, TotalWeight);
	for index = 1, TotalWeight do
		ticket = ticket - Weights[index]
		if ticket <= 0 then
			selected = PlayingPlayers[index]
	return selected;

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
				table.insert(PlayingPlayers, Player)
		TopText.Value = 'GameTimeText'
		local Map = ChosenMap:Clone()
		Map.Parent = workspace
		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.


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