Random Player Selector not functioning properly

I am trying to make it so different people get hunter each match, here’s my code so far but for some reason it doesn’t include everybody in it or doesn’t include anyone:

function shufflePlayers(arr)
	local array = table.clone(arr)
	local m = #array
	local t, i

	while m > 0 do
		i = math.floor(math.random() * m)
		t = array[m]
		array[m] = array[i]
		array[i] = t
		m = m - 1
	end

	return array
end

My usage:

for i, player in ipairs(shufflePlayers(players)) do
		if i <= hunterCount then
			player.Team = game.Teams.Hunter
			table.insert(Hunters, player)
		else 
			player.Team = game.Teams.Runner
			table.insert(RunnersAlive, player)
			table.insert(Runners, player)
		end
	end

This line can set i to 0, and because Lua arrays are 1-indexed, it will make your ‘array’ table into a dictionary (actually part array, part dictionary most of the time) and ipairs will not iterate over it completely. Try changing this line to:

i = math.random(m)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.