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.
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)