Are coroutines useful in this scenario?

Hi everyone!

I’m making an announcement system, which sends a notifications to all players on a textlabel. Would you say that in this scenario “coroutines” would be useful, since I call the tween for every player.

Code:

luau text code:

local TS = game:GetService("TweenService")
local Players = game:GetService("Players")
function sendNotificationToall(msg)
    for _, v in pairs(Players:GetPlayers()) do
        -- Are coroutines useful in this case?
        -- because we use TweenService for each player
        coroutine.wrap(function()

            local tween = TS:Create(v.PlayerGui.GUI.TextLabel, TweenInfo, Goal)
            tween:Play()
            task.wait(tween.TweenTime)
            -- We use a yield here, to wait for the tween to finish
            -- Task.wait and then there is more code below
        end)()
    end
end

sendNotificationToall("Hello how are you?")

All answers are appreciated! Thank you!

1 Like

sorry for not asnwering , how u make ui like that ? looks dope !

1 Like

So the technical answer is yes!

However…

You shouldn’t need separate threads on the same machine here because manipulating PlayerGui from the server is generally considered a hack. It’s a Roblox-exclusive engine quirk, it doesn’t make it wrong, but it does make it weird. Instead please consider pushing a notification to each client user’s machine instead (do what you like, I’m just advocating for general developer preference btw).

I know this doesn’t exactly elaborate on coroutines- so I will also leave this. Any time your thread is going to receive hinderance because of yielding where seemingly dual execution is needed, coroutines/thread spawning are never a question of if they are useful! I recommend reading about task.spawn and coroutine individually and deciding where you prefer to use each (generally, when the thread needs to pause execution you would use a coroutine).

3 Likes

Hey, I actually used “Visual Studio Code” for this. The plugin is called: “Code Snippet”

3 Likes

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