Managing Tycoon Dropper Times

Hi, So right now I’m currently working on a Dropper Type System for a Game, and I was wondering about Properly Managing Dropper Times, and when to fire them.

Most of the time when I see Tycoon Games, their Droppers Contain a Script Within every single one of them, and they contain a while loop inside of them telling them when to drop, something like this:

while task.wait(script.Parent:GetAttribute("Rate")) do -- loops
    DropObject:Invoke(i, DropType)
    -- Typically three is no BindableFunction
end

Pretty Simple, but not very efficient in the long run.

So Im my Attempts, I have a Script that Tags an Object Under a Specific Name, and gives it a piece of Data to use when it comes for that usage, The Following is essentially the Script that Controls the Droppers, and their Data:

(I used Parallel Luau here, but I dont know if it is a good utilization of it)
(It is also Managed by the Server, with a few Exceptions)

local Tag = "Droppers" -- Tag to use

CollectionService:GetInstanceAddedSignal(Tag):ConnectParallel(i)
    if not i:IsA("BasePart") then -- To Ensure we are getting the right objects
        task.synchronize() -- Runs in Serial
        i:RemoveTag(Tag) -- Removes Tag
        return
    end
    DropperData[i] = {os.clock(), i:GetAttribute("Rate")} -- Adds Data
end)


RunService.Heartbeat:ConnectParallel(function()
    local Collection = CollectionService:GetTagged(Tag) -- Gets Instances
    if #Collection < 1 then return end -- Stops if Collection is Empty

    for _,i in next, Collection do -- iterates through Collection
        local Data = DropperData[i] -- Gets Dropper Data
        if not Data then continue end -- Skips if no Dropper Data

        local Elapsed = os.clock() - (Data[1] or 0) -- Elapsed Time
        if Elapsed < Data[2] then continue end -- Skips if time not reached

        local DropType = i:GetAttribute("Type") -- Grabs Dropper Type
        Data[1] = os.clock() -- Resets Time
        
        task.synchronize() -- Runs in Serial
        DropObject:Invoke(i, DropType) -- Invokes BindableFunction to Drop Item
    end
end)

It appears to work as Intended, but I’m wondering if this is the best way to actually do this? Because I have seen other methods where people use Object Oriented Programming for this.

Would this be better?

This is a very valid approach to managing a consistent sequence of events. However, I would not use parallel luau for this. Parallel luau is designed for computationally heavy workloads, I.E. procedural terrain generation. You can use OOP but the reasons for using it for something like this will be opinionated rather then concrete reasons not too. One change that I would also do is have all the dropper data be stored in attributes instead of having a mix of both. You wont need to manage a dropper data table but instead index the instance directly for the start os.clock along with the rate.

Side question, what is your reasoning for using next here?

2 Likes

I’m not sure about this one, Using Attributes sends more Networking Data than having it as a table, which is what something I’m trying to avoid doing.

Probably not too important in the long run, but a lot of things seem to make it important when it comes to Server stuff

Not sure now that I think about it.

It might send more network data but it is nowhere near enough to cause any issues.

I guess so, but thank you for helping.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.