Hello, ScriptingHelpers!
Sometimes, my tycoon collector would generate too much money from money bricks.
The thing is that sometimes the amount of generated money (shown on the GUI where players collect money) would jump from 10k, 20k to 1M, which is actually something like 2000000142.
I've been told that debounce could be a fix, but I'm not sure how to add debounce to my script.
SCRIPT WHICH CONVERTS DROPPED BRICKS TO MONEY:
script.Parent.Essentials.Collector.Touched:connect(function(hit) if hit:FindFirstChild("Cash") then script.Parent.Cash.Value = script.Parent.Cash.Value + hit.Cash.Value Instance.new("Sparkles",hit).Color=Color3.new(math.random(1,255)/255,math.random(1,255)/255,math.random(1,255)/255) game.Debris:AddItem(hit,0.1) end end)
touched fires insanely without a debounce. It keeps firing so long as the player stays in contact. Debounce is a variable, which is set to true and false when the event fires. The best way to show this is through an example.
local debounce=false--declaring script.Parent.Essentials.Collector.Touched:Connect(function(hit) if debounce==false then --checking debounce=true--setting true if hit:FindFirstChild("Cash") then script.Parent.Cash.Value = script.Parent.Cash.Value + hit.Cash.Value Instance.new("Sparkles",hit).Color=Color3.new(math.random(1,255)/255,math.random(1,255)/255,math.random(1,255)/255) game.Debris:AddItem(hit,0.1) end wait(3)--change to whatever u want in seconds. debounce=false--setting to false end end)
Oh and also use Connect
I used to have a that problem but after i used this code, it works with out a debounce.
script.Parent.Essentials.Collector.Touched:connect(function(hit) if hit:FindFirstChild("Cash") and hit:FindFirstChild("Selling") == nil then local Tag = Instance.new("BoolValue") Tag.Name = "Selling" Tag.Parent = hit script.Parent.Cash.Value = script.Parent.Cash.Value + hit.Cash.Value Instance.new("Sparkles",hit).Color=Color3.new(math.random(1,255)/255,math.random(1,255)/255,math.random(1,255)/255) game.Debris:AddItem(hit,0.1) end end)