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

How do I make my "Touch For Cash" Not spam cash?

Asked by
i_Rook 18
3 years ago

I am currently making a new game, and I am making a obby that can get you coins, but my script keeps on spam giving me cash! What did I do wrong?

amnt = 500 
function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if (h~=nil) then
        local thisplr = game.Players:findFirstChild(h.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local coins = stats:findFirstChild("Coins")
                if (coins~=nil) then
                    coins.Value = coins.Value + amnt
                end
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)

Please help me. Thanks!

2 answers

Log in to vote
0
Answered by 3 years ago

As the player walks over the part, their limbs are constantly leaving and touching the object. What you could do is a add a debounce so that it won't register that player's hit's until a certain amount of time passed.

local Cooldown = {}
Object.Touched:Connect(function(Obj)
    if Cooldown[Obj.Parent] then return end
    Cooldown[Obj.Parent] = true

    wait(1)

    Cooldown[Obj.Parent] = nil
end
Ad
Log in to vote
0
Answered by 3 years ago

You can simply add a debounce, this is sample code and I'll explain it.

part.Touched:Connect(function(t)
    print("Touched")
end)

This will spam because it has mutliple parts constantly touching it. To get around this, you simply add a value that will enable / disable itself with a cooldown like done below: local debounce = false

part.Touched:Connect(function(t)
    if debounce == false then
        print("Touched")

        debounce = true
        wait(1)
        debounce = false
    end
end)

Answer this question