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

How to stop a button from being registered as pressed twice?

Asked by
Scerzy 85
8 years ago

Alright this is a simple problem, and should have a simple solution, but I cannot figure it out. The script below works perfectly, the button even fades away like I want it to(lines 26-29). However, if a player steps on the button again as the button is fading, the button will register as being pressed again and will subtract its price from the leaderstats once again. As a result, someone might pay double or triple the price for an object.

Any help on a solution to this would be appreciated. I just need to know the general direction that I need to go in to keep the button from being pressed more than once while fading in that one second of vulnerability. Thanks.

-------------------------
--Background info
--script.Parent is the button
--script.Parent.Parent is a model of all the buttons
--script.Parent.Parent.Parent is the whole tycoon model
--script.Parent.Parent.Parent.Products is a model of all the products that the buttons buy
-------------------------

repeat wait() until script.Parent.Product.Value~=nil
local product=script.Parent.Product.Value:clone()
script.Parent.Product.Value:remove()
local price=script.Parent.Price.Value

script.Parent.Name=script.Parent.ProductName.Value.." - "..price
if price==0 then
    script.Parent.Name=script.Parent.ProductName.Value.." - Free!"
end

script.Parent.Head.Touched:connect(function(hit)
    chr=hit.Parent
    if chr:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(chr) then
        plr=game.Players:GetPlayerFromCharacter(chr)
        if plr.Name==script.Parent.Parent.Parent.OwnerName.Value and plr.leaderstats.Money.Value>=price then
            plr.leaderstats.Money.Value=plr.leaderstats.Money.Value-price
            product.Parent=script.Parent.Parent.Parent
            for i = 1, 10 do
            script.Parent.Head.Transparency = script.Parent.Head.Transparency + .1
            wait(.1)
            end
            script.Parent:remove()
        end
    end
end)

2 answers

Log in to vote
1
Answered by
pwnd64 106
8 years ago
-------------------------
--Background info
--script.Parent is the button
--script.Parent.Parent is a model of all the buttons
--script.Parent.Parent.Parent is the whole tycoon model
--script.Parent.Parent.Parent.Products is a model of all the products that the buttons buy
-------------------------

repeat wait() until script.Parent.Product.Value~=nil

local product=script.Parent.Product.Value:clone()

script.Parent.Product.Value:remove()

local price=script.Parent.Price.Value

script.Parent.Name=script.Parent.ProductName.Value.." - "..price

Debounce = false --A variable we can use to tell if something has happened.



if price==0 then

    script.Parent.Name=script.Parent.ProductName.Value.." - Free!"

end

script.Parent.Head.Touched:connect(function(hit)

        chr=hit.Parent

        if chr:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(chr) then

            plr=game.Players:GetPlayerFromCharacter(chr)

            if plr.Name==script.Parent.Parent.Parent.OwnerName.Value and plr.leaderstats.Money.Value>=price and Debounce == false then --Checking if the button has been pressed before.

        Debounce = true --Tells the system the button has been pressed.

                plr.leaderstats.Money.Value=plr.leaderstats.Money.Value-price

                product.Parent=script.Parent.Parent.Parent

                for i = 1, 10 do

                    script.Parent.Head.Transparency = script.Parent.Head.Transparency + .1

                    wait(.1)

                end

                script.Parent:remove()

            end

        end

end)

Try this. What we do is add a debounce, which will make it so that, when pressed, it checks if the button has been pressed before, and if not it runs the code and makes it not able to run again.

0
Thanks a lot, this is my first time being introduced to debouncing. This will come in handy. Scerzy 85 — 8y
Ad
Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

This is a very common problem solved by a very common tactic: debounce.

I am on mobile so I can't give you the full code, but the gist of it is here:

local deb = false

script.Parent.Head.Touched:connect(function(hit)
    if deb then return end
    deb = true
    -- If you intended to use this function again, you would have to set 'deb' back to false.
end)
0
Thank you, this explains it just as well as the other answer. Scerzy 85 — 8y

Answer this question