Connections Threads

I heard that every Connection creates a new thread so here is my question:

when a listener ends executing some code what happens to the thread that has been created?

does it get Destroyed or not?

if it’s not destroyed then how should I destroy it since I know that a lot of threads in a game are a bad thing?

and is it a bad thing to fire remotes rapidly?

if I have some misconceptions I would highly appreciate any help to correct myself.

You can use the :Disconnect() function. Example:

plrAdded = game.Players.PlayerAdded:Connect(function(plr)

end)

plrAdded:Disconnect()
1 Like

that disconnects the listener that creates more threads but what about the already created threads?

This will destroy the thread. It will stop firing.

1 Like

Try running this code:

thread = game:GetService("RunService").Stepped:Connect(function()
	print"still active"
end)

wait(10)
thread:Disconnect()

After 10 seconds, the thread is destroyed, and it stops printing.

1 Like

its great to know that disconnecting a listener destoryes the already made threads,

but what if im using the remotes to update information about the clients every once in a while? for example i have a table in module script that has all players information like if they are attacking or blocking an attakc…Etc, so im using a remote event to Update this table every once in a while but i dont want the threads that has been created to stack up? so how should i go about this?

i mean i want to destory the threads after they finish excuting.

Doing this would mean that you don’t ever want the listener to run again once it runs once. Is that what you need?

what i meant is that i dont want to disconnect the listener since i need it to update information on the server but at the same time when it fires it creates new thread so is there a way to destory the threads without disconnecting the listener?

in other words destroy the thread once it finishes excuting without the need to disconnect the listenrer

I think this is done automatically, and shouldn’t slow down your game.

1 Like

What do you mean exactly by “threads”

1 Like

you can find it here

Just a quick FYI, roblox doesn’t support anything in parallel (like multithreading), it simply switches from one task to another, but it only switches when the current thread yields. But what you’re referring to is a completely different thing. Starting a new connection don’t create a new thread, because they are two different things

To add up, Luau is releasing soon and Roblox will be multithreaded soon.

2 Likes

listeners do create new threads, and about if Lua supports anything in parralel or
not then I’m not sure about it, still need to do my research but i guess your right about lua not supporing parallism since i read couple topics a while back talking about it

Luau is actually live in studio, but not live in real games. Luau is still unstable and the goal is to boost performance along with backwards-compatible with current Lua.

2 Likes

I don’t know what kind of threads he is referring too, but the kind I know don’t exist on roblox yet.

1 Like