Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Player touching something too many times and is causing bugs?

Asked by 18 days ago
Edited 18 days 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 359 — 18d
0
*more than once ImTrev 359 — 18d
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 30 — 18d

1 answer

Log in to vote
0
Answered by 18 days ago
Edited 18 days 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 30 — 18d
0
Try this Official_DuckyYT 55 — 18d
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 30 — 18d
0
do .Value, I was just testing Official_DuckyYT 55 — 18d
Ad

Answer this question