0
\$\begingroup\$

My script for a slot machine isn't working. When the button in the Bilboard GUI is clicked, the below dosen't run. I am new to scripting and this is mostly based on wiki/stack exchange/ Dev page info.

local player = game.Players.LocalPlayer
local textLabel = script.Parent 
local toggled = false
local button = script.Parent

textLabel.Text = "Click to Play for 5 Cash!"
slot1Combos = {"Bar", "Seven", "Seven", "Seven", "Cherry", "Cherry", "Cherry", "Cherry", "Orange", "Orange", "Orange", "Orange", "Orange", "Lemon", "Lemon", "Lemon", "Lemon", "Lemon", "Banana", "Banana", "Banana", "Banana", "Banana"} 
slot2Combos = {"Bar", "Seven", "Cherry", "Cherry", "Cherry", "Orange", "Orange", "Orange", "Orange", "Orange","Orange","Lemon", "Lemon", "Lemon", "Lemon", "Lemon", "Lemon", "Banana", "Banana", "Banana", "Banana", "Banana", "Banana"} 
slot3Combos = {"Bar", "Seven", "Cherry", "Cherry", "Cherry", "Orange", "Orange", "Orange", "Orange", "Orange","Orange","Lemon", "Lemon", "Lemon", "Lemon", "Lemon", "Lemon", "Banana", "Banana", "Banana", "Banana", "Banana", "Banana"} 

local function onButtonActivated()
    if toggled == false then
        toggled = true  

        x = math.random(1,23)
        y = math.random(1,23)
        z = math.random(1,23)
        g = false
        player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value - 5
        if slot1Combos[math.floor(x)] == "Bar" and slot2Combos[math.floor(y)] == "Bar" and slot3Combos[math.floor(z)] == "Bar" then 
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 60
            textLabel.Text = "You Got 3 Bars. You won 60 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"
        elseif slot1Combos[math.floor(x)] == "Seven" and slot2Combos[math.floor(y)] == "Seven" and slot3Combos[math.floor(z)] == slot2Combos[math.floor(y)] then 
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 40
            textLabel.Text = "You Got 3 Sevens. You won 40 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"
        elseif slot1Combos[math.floor(x)] == slot2Combos[math.floor(y)] and slot3Combos[math.floor(z)] == slot2Combos[math.floor(y)] then 
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 10
            textLabel.Text = "You Got 3 of a Kind. You won 10 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"

        elseif slot1Combos[math.floor(x)] == "Cherry" and slot2Combos[math.floor(y)] == "Cherry" and slot3Combos[math.floor(z)] == "Cherry" then 
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 20
            textLabel.Text = "You Got 3 Cherries. You won 20 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"
        elseif slot1Combos[math.floor(x)] == "Cherry" and slot2Combos[math.floor(y)] == "Cherry" or slot3Combos[math.floor(z)] == "Cherry" and slot1Combos[math.floor(x)] == "Cherry" or slot2Combos[math.floor(y)] == "Cherry" then
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 3
            textLabel.Text = "You Got 2 Cherries. You won 3 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"
        elseif slot1Combos[math.floor(x)] == "Cherry" or  slot2Combos[math.floor(y)] == "Cherry" or  slot3Combos[math.floor(y)] == "Cherry" then
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 3
            textLabel.Text = "You Got 1 Cherry. You won 3 cash"
        else 
            textLabel.Text = "Sorry, you didn't win"
            wait (5)
            textLabel.Text = "Click to Play for 5 Cash!"


        end
    end
button.Activated:Connect(onButtonActivated)
end)

This is the explorer tree with the script highlighted

explorer tree

\$\endgroup\$
1
\$\begingroup\$

Taking a look at your script, we see that there is a little hick-up at the bottom of your function.

local function onButtonActivated()
    if toggled == false then
        toggled = true  

        x = math.random(1,23)
        y = math.random(1,23)
        z = math.random(1,23)
        g = false
        player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value - 5
        if slot1Combos[math.floor(x)] == "Bar" and slot2Combos[math.floor(y)] == "Bar" and slot3Combos[math.floor(z)] == "Bar" then 
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 60
            textLabel.Text = "You Got 3 Bars. You won 60 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"
        elseif slot1Combos[math.floor(x)] == "Seven" and slot2Combos[math.floor(y)] == "Seven" and slot3Combos[math.floor(z)] == slot2Combos[math.floor(y)] then 
            [...]
        else 
            textLabel.Text = "Sorry, you didn't win"
            wait (5)
            textLabel.Text = "Click to Play for 5 Cash!"


        end
    end
button.Activated:Connect(onButtonActivated) -- <- !!!This here!!!
end)

Lua code does not have to be indented, it is indented to ease the reading of the code. Your code being cleanly indented, we can assume that the last end is the one that ends your function onButtonActivated.

From that, we can infer that the part button.Activated:Connect(onButtonActivated), which links your button to the function, is being part of your onButtonActivated function.

Essentially, you're trying to link the function to your button from within the function, which would happen only if you would call the function in the first place.

What you most likely want here is to link the button to the function from the outside of the function, after the function has been declared:

local function onButtonActivated()
    if toggled == false then
        toggled = true  

        x = math.random(1,23)
        y = math.random(1,23)
        z = math.random(1,23)
        g = false
        player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value - 5
        if slot1Combos[math.floor(x)] == "Bar" and slot2Combos[math.floor(y)] == "Bar" and slot3Combos[math.floor(z)] == "Bar" then 
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 60
            textLabel.Text = "You Got 3 Bars. You won 60 cash"
            wait(5)
            textLabel.Text = "Click to Play for 5 Cash!"
        elseif slot1Combos[math.floor(x)] == "Seven" and slot2Combos[math.floor(y)] == "Seven" and slot3Combos[math.floor(z)] == slot2Combos[math.floor(y)] then 
            [...]
        else 
            textLabel.Text = "Sorry, you didn't win"
            wait (5)
            textLabel.Text = "Click to Play for 5 Cash!"
        end
    end
end

button.Activated:Connect(onButtonActivated) -- <- !!!Moved outside of the function!!!

The closing parenthesis sicked to the end at the end of your code snippet above could be causing a syntax error (unexpected symbol). Chances are you have that reported somewhere. Just remove it, you'll be fine :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.