Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My button isn't working when I don't have the desired amount?

Asked by 5 years ago

I have a tycoon button that when pressed, takes away the amount that is specified in a value. The problem is that if I step on the button and I don't have enough money, it won't work when I do.

local cost = script.Parent.cost

game.Players.PlayerAdded:Connect(function(player)
    local debounce = true
    script.Parent.Touched:Connect(function()
        local plrstats = player.leaderstats.Money
        if debounce then
            debounce = false
            if plrstats.Value >= cost.Value then
                plrstats.Value = plrstats.Value - cost.Value
                wait(10)
            debounce = true 
            end
        end
    end)
        end)

This is a representation of the model:

button
     basepoint
         buyscript (the script above)
         cost (number value)
    stand (just a part below the basepoint for design)

The Lua code above is not a script or line of code, just a recreation of the model in the explorer.

The script is made to work so that when a player touches the button, it finds the value of "cost" (the price for say, a wall of a tycoon) and if the player has more or equal to the desired amount, take the money away. The debounce is obviously just the cooldown for the button.

1 answer

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago

Its because you are setting the debounce to false everytime you touch the brick, no matter if they have enough money or not, so when they dont have enough money, they step on the brick and the debounce will change to false, then when they have enough money and step on the brick, well it wont make it through the debounce checking since debounce is already false

change the debounce boolean when the player has enough money and it will work

local cost = script.Parent.cost

game.Players.PlayerAdded:Connect(function(player)
    local debounce = true
    script.Parent.Touched:Connect(function()
        local plrstats = player.leaderstats.Money
        if debounce then
            debounce = false--this is the part where you made a mistake
            if plrstats.Value >= cost.Value then
                plrstats.Value = plrstats.Value - cost.Value
                wait(10)
            debounce = true 
            end
        end
    end)
        end)

Im pretty sure you know what to do now

Ad

Answer this question