How could I make this give money like a slot machine efficiently?

I want to make this give money like a slot machine where depending on the image and the amount it gives you money differently, how could I do this without a trillion lines?

(I cut the code up to important things only)

local images = { 
	"rbxassetid://13433193234", -- Jackpot 7 
	"rbxassetid://13433283985", -- Orange
	"rbxassetid://13433253339", -- Cherries
	"rbxassetid://13433301182" } -- Bell

	local numberL = math.random(1, #images)
	local numberM = math.random(1, #images)
	local numberR = math.random(1, #images)
			
		for j = 1, 3 do
			for i = 1, 50 do
				wait(0.025)
				spinnerList[j].SpinImage.Texture = images[math.random(1, #images)]
			end
				
			if j == 1 then
				spinnerL.SpinImage.Texture = images[numberL]
			elseif j == 2 then
				spinnerM.SpinImage.Texture = images[numberM]
			elseif j == 3 then
				spinnerR.SpinImage.Texture = images[numberR]
			end
				
			clickSound:Play()
		end
						
		spinningSound:Stop()
			
		wait(1.25)
			
		if numberL == numberR or numberL == numberM or numberM == numberR then
			win(numberL, numberM, numberR)
		else
			lose()
		end

     function win(numberL, numberM, numberR)
     	     print(numberL, numberM, numberR)
	
     end

1 Like

Just create another table like:

local payout = {1000, 500, 100, 50}
and in win if the number is 1-4 print(payout[num])

You only need to send in 1 number in the function as they are all equal by your if statement

The way slots payout is through patterns and combinations though.

This is something that would work if all 3 numbers are the same, but this doesn’t account for combinations where only 2 images match:

function win(numberL, numberM, numberR)
	print(numberL, numberM, numberR)
	
	local payoutMultiplier = 1
	
	if numberL == numberM and numberM == numberR then
		if numberM == 1 then
			payoutMultiplier = 10
		elseif numberM == 2 or numberM == 3 then
			payoutMultiplier = 3
		elseif numberM == 4 then
			payoutMultiplier = 5
		end
	end
end

Your code was specific to only calling win if all 3 numbers were the same. Therefore you should just have 1 function not a win and a lose but like check. In check you could something like the following:

function check(numberL, numberM, numberR)
    local tbl = {numberL, numberM, numberR}
    table.sort(tbl)
    local result = table.concat(tbl, "", 1, 3)
    local win = 0
    local multiplier = payout[result]
    if multiplier then
        win = bet * multiplier
    end
    return win
end

Then just define a dictionary called payout:

payout = {
    ["111"] = 20,
    ["222"] = 16,
    ["122"] = 16,
    .............
}

You just list all the results you would like to give a payout for.

1 Like