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

Player touching something too many times and is causing bugs?

Asked by 3 years ago
Edited 3 years ago

So I am trying to make my own tycoon from scratch and I only have to make the collector where you touch it and get the money, and at first it was working fine, but something happened (Which I'm not sure what happened) and it gives the player double the money every time they touch it. Here is an example of the code

local Button = script.Parent
local CashToCollect = script.Parent.Parent.Parent.PartCollector.CashToCollect

local debounce = false
local timer = 2
Button.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        if hit.Parent:FindFirstChild("Humanoid") then
            if hit.Parent.Name == script.Parent.Parent.Parent.OwnerDoor.Owner.Value then
                local Player = game.Players:FindFirstChild(script.Parent.Parent.Parent.OwnerDoor.Owner.Value)
                Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + CashToCollect.Value
                CashToCollect.Value = 0
            end
        end

        wait(timer)
        debounce = false
    end
end)

` If someone could help it would be greatly appreciated.

0
Try adding a print statement after line 10 and see if it prints 2 times. If it does then I can further help then. ImTrev 344 — 3y
0
*more than once ImTrev 344 — 3y
0
I put print(hit) after line 10 and at first I thought it would print both legs, but it only prints right leg but it stills double the money received GooseStranger 27 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try this

local Button = script.Parent
local CashToCollect = 100

local debounce = false
local timer = 2
Button.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
                 local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
                if plr then
                  plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + CashToCollect
                CashToCollect.Value = 0
                end

        wait(timer)
        debounce = false
    end
end)
0
I tried this and it still doubled the amount you would normally get GooseStranger 27 — 3y
0
Try this Official_DuckyYT 55 — 3y
0
So I put in the code and made some adjustments to fix my needs, and it does the same thing it had done before: it would give the correct amount of cash but it gives an error that says "Workspace.Tycoon.Essentials.Collector.CollectorPart.Script:11: attempt to perform arithmetic (add) on number and Instance" GooseStranger 27 — 3y
0
do .Value, I was just testing Official_DuckyYT 55 — 3y
Ad

Answer this question