A few issues with my tyccon

In the link above, there are comments that highlight 2 issues:

  1. If wait() is less than 3, all parts that have been dropped will be destroyed, and it wouldn’t add 1 to Coins (an IntValue).

  2. If wait() is greater than or equal to 3, instead of adding 1 to Coins (an IntValue), it adds by 2, then 3, and so on.

There are two clips that show these issues:

Clip 1: https://www.youtube.com/watch?v=s90IN1N8xfg&feature=youtu.be
Clip 2: https://www.youtube.com/watch?v=4Hg9UQgeGwA&feature=youtu.be

How can I resolve these two issues?

Your issue is that you have nested the .Touched event inside of your while wait(t) do loop.

Why do you have a while loop anyway? What is it achieving?

Every 5 seconds (or however long you specify the wait time to be) the script will add another function bound to the .Touched() event of the collector. This would mean that over time, more and more functions are ran when one part touches the collector and multiple coins are added as a result.

The while loop is to drop a part using Instance.new.

Okay, well the event with the .Touched() should not be inside the loop. This should fix your issue, I believe.

In my experience with tycoons, the .Touched() event for the collector can easily be placed in a seperate script to handle collections, and each dropper can have it’s own script to continuously loop and drop a part.

1 Like

What I suggest:

local Collector = script.Parent.Parent.Parent.Conveyor.Collector
local Coins = game.ReplicatedStorage.Coins

local function addCoins(part)
    part:Destroy()
    Coins.Value = Coins.Value + 1
end

local denounce = false
Collector.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        if hit and hit.Name == "MoneyPart" then
            addCoins(hit) 
        end
        wait(0.5)
        debounce = false
    end
end)


while wait(5) do
    local part = Instance.new("Part",game.Workspace)
    local debounce = false 
    part.Size = Vector3.new(1, 1, 1)
    part.Position = script.Parent.Position - Vector3.new(0, 2, 0)
    part.Name = "MoneyPart"
end

Please set my previous answer as a solution if this solved your issue! :smiley:

1 Like

Thanks for the solution! I appreciate it.

1 Like