How to queue events? (add arguments to queued functions)

Hello! I need to show messages from the server via remote events and display them. But the thing is I don’t want to miss any and so I need to queue them. I found an answer from this post, but I don’t know how to pass arguments through it. Here is the code I am using right now

local function add (plrName,itemOrCash,item,amount,sign)

	queue:QueueTask(addQ(plrName,itemOrCash,item,amount,sign))
    --this runs instantly

end
game.ReplicatedStorage.BE.Add.Event:Connect(add)
game.ReplicatedStorage.RE.Add.OnClientEvent:Connect(add)

or this

local function add (plrName,itemOrCash,item,amount,sign)

	queue:QueueTask(addQ)
    --this runs in a queue, but doesn't have arguments so it won't work

end
game.ReplicatedStorage.BE.Add.Event:Connect(add)
game.ReplicatedStorage.RE.Add.OnClientEvent:Connect(add)

But if I add arguments and the parentheses “()”, it will run instantly. If I don’t put the parentheses, as in the example from this post, then it won’t have the arguments. The arguments are necessary for the message. To restate the problem, I’m trying to add arguments to the queued function. If you read this, please help me since I’ve been working on this for a week already. Thank you so much!

2 Likes

I really don’t think you need to queue them, but if you think it will help here’s a version of the queuing module that allows for function arguments.

local Queue = {}
Queue._mt = {__index = Queue}

function Queue.new()
    local self = setmetatable({}, Queue._mt)
    self.pendingTasks = {}
    self.running = false
    return self
end

function Queue:QueueTask(...)
    table.insert(self.pendingTasks, {...})
    if not self.running then
        self:Run()
    end
end

function Queue:Run()
    self.running = true
    spawn(function()
        while #self.pendingTasks > 0 do
            local task = table.remove(self.pendingTasks, 1)
            task[1](table.unpack(task, 2))
        end
        self.running = false
    end)
end

return Queue
local queue = require(QueueModule).new()
queue:QueueTask(print, "Test")
1 Like

you don’t need to use parentheses () in function that call another function
examplefunction( myfunc , myfunc_arg1 , myfunc_arg2 )

local function add (plrName,itemOrCash,item,amount,sign)
       -- this how to do it in your code
	queue:QueueTask(addQ,plrName,itemOrCash,item,amount,sign)
    --this runs instantly

end
game.ReplicatedStorage.BE.Add.Event:Connect(add)
game.ReplicatedStorage.RE.Add.OnClientEvent:Connect(add)