Sometimes the cash collector would simply jump from like 10k, 20k to 1M+ and after collecting, it gives a ridiculously huge amount of money, like 19999999412.
It usually happens when tycoon is nearly finished or finished.
I’ve checked the scripts which convert dropped money to real cash, but there’s no part of script which would give this huge amount, it is supposed to give only the amount the dropper is set to give.
Maybe is there a way to log every single time when cash is added to the collector so I know what causes this?
If I could take a guess, I would assume the Touched event you’re using on the money collector has no debounce. Basically, without a debounce, even if it’s only one single part that touches the collector, that Touched event can very well run multiple times, even if it destroys the part after executing some code.
Debounce prevents this, and it’s really easy to implement. Here’s an example of debounce:
local DB
script.Parent.Touched:Connect(function(hit)
if not DB and hit.Name == "Money" then --Make sure nothing is already running before we start a new execution
DB = true
hit:Destroy()
DB = false
end
end)
At the moment we are guessing what the issue with your code is. Please could you provide the section of your code that the issue may be coming from and a little information about your set up.
I completely agree with the others about adding a debounce as this could be the thing that your code is missing. You did say this issue usually happens when the tycoon nearly finishes so this brings me to think it could be an issue with a dropper giving too much money but this seems very unlikely.
This process is done by PurchaseHandler, which also handles other things for my tycoon.
I also believe I need to add debounce but I’m not sure where I need to place debouncing strings…
This is how the script converts dropped bricks to cash:
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)
Like mentioned previously, I would recommend a debounce. @Nightrains I’m not sure why a debounce would require yielding. Any form of debounce should act instantly unless the condition is run in separate threads. Here’s an example of where a problem could form:
local debounce
spawn(function() -- Makes a new thread. Could just be an event connection
debounce = true
end)
-- The debounce is used in separate threads!
if debounce then
return
end
-- Code continues executing even though the debounce was set because it gets set after this if statement
As for the @Superstarer1234’s issue,
Based on your code I would recommend simply using hit.Cash:Destroy() after the value is added on line 3.
script.Parent.Essentials.Collector.Touched:connect(function(hit)
if hit:FindFirstChild("Cash") then
script.Parent.Cash.Value = script.Parent.Cash.Value + hit.Cash.Value
hit.Cash:Destroy() -- This will prevent the above if statement from executing code here
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)
It already removes the bricks when it touches the Collector, it simply sometimes gives too much money, like most people think the Touched event is done too many times.
I think I need to implent Debounce somehow on this script so the event runs ONLY once when brick touches.
Due to this glitch, sometimes the GUI where generated money appears, it’ll jump from like 10k or 20k to 1M, which after claiming is a number like 1999999417
I also believe that I need to add Debounce, but I’m not sure how do add it. I attempted to add it by myself, however the issue won’t go away…
Here’s the code:
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)
The script I used to try to fix:
local debounced
script.Parent.Essentials.Collector.Touched:connect(function(hit)
if not debounced and hit:FindFirstChild("Cash") then
debounced = true
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)
debounced = false
end
end)
The debounce is used for the function to be executed after each debounce, if the function does not have any yielding before setting debounce = false, then the debounce will be instantaneous. In your example you use the spawn() function and that is already an exception because spawn() has a delay of one second so that would count.
Also creating new threads unless just adding wait() is very unnecessary.
Thank you for responding with your code because this has allowed me to understand what your issue is. After a bit of testing I was able to reproduce your issue with the code you provided and came up with simple fix. Adding a DeBounce in this scenario and the current set up you have wont actually solve your issue but make another issue.
After a little bit of testing I found out if you made the cash value nil once it has been collected and then check to see if the cash value is not nil in the if statement it will fix your issue. This isn’t the best solution in the world but it is a solution and it seems to work. Here is your code fixed:
script.Parent.Essentials.Collector.Touched:connect(function(hit)
local Cash = hit:FindFirstChild("Cash")
if Cash and Cash.Value ~= nil 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)
Cash.Value = nil
game.Debris:AddItem(hit,0.1)
end
end)
For context, destroying the cash part doesn’t seem to fix it and I found this to be the only solution for the time being.
It removes the brick and it removes it after 0.1 seconds. That’s the issue. That’s also why your debounce did not work. Your debounce is reset before the part is destroyed. It can easily touch the collector multiple times which is why I said to remove the Cash value in the part not the part.
After trying your code, it still sometimes gives too much money, but now not as many times as before, it happens fewer times now. Is this why you said that the solution isn’t best in the world?
Hmm, that is strange because it should be working as I tested my solution with parts spawning every 0.03 seconds and it is all working fine. Are you sure it isn’t a dropper giving out too much money?
Nope, all the droppers drop only from 5-20 coins. Maybe I need to increase dropping interval alittle so the collector isn’t rarely glitching at all if all droppers are built?
Maybe I need to make the dropped brick to disappear faster?