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

Player touching something too many times and is causing bugs?

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.

3 comments

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.

0

*more than once

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

1 answer

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)

4 comments

0

I tried this and it still doubled the amount you would normally get

0

Try this

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"

0

do .Value, I was just testing

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