Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.