Help with a table in script

I don’t see anything wrong with the code. Is there anything in the output? Try adding prints, see if the code is running.

function rouletteSelection(players)
	local total: number = 0
	for key, chance in pairs(players) do
		total += chance
	end
    -- print("Running roulette selection")

Make sure you didn’t run the function while the players table is empty.

the function isn’t running :/, what do i do?

Hi is this a ModuleScript or a ServerScript?

It’s a ModuleScript, please help me

Give this a shot:

local function rouletteSelection()
	local total = 0

	for key, chance in pairs(Players:GetPlayers()) do
		total += chance
	end

	local random = math.random()
	local runningTotal = 0
	local chosenPiggy

	for player, chance in pairs(Players:GetPlayers()) do
		local probability = chance / total
		runningTotal = runningTotal + probability

		if runningTotal > random then
			chosenPiggy = player
			break
		end
	end

	return chosenPiggy
end

still doesn’t work :frowning: i think we are close tho

Have you made sure to run module.ChoosePiggy when the selectChapter function ends?

Remember that you’ve already declared it on local Piggy: string = rouletteSelection(), and it should return a string. You don’t have to run that function independently.

If it still doesn’t work, could you provide a video of the Studio interface with the output while you run the game as normal?

What do you mean by “string”? How do I not run the function indipendently?

function returnsmth()
	local string = "cat"
	return string
end

local string = returnsmth()
print(string) -- "cat"
-- when declaring a variable as a function, the variable
-- is what the function returns

about non indepently, not sure about the term, but just don’t run it, but setting the variable to the function