ServerScript Functions works as separate threads?

Hiwi!

Question, “Player1” clicks a part and a Server Script start to Spawn Parts in server, the Parts got name from the Player1.Name
An average spawn time of 0.5 seconds per part, so it’s 15 seconds to finish spawning.
Server Script

local function spawnParts(player)
	local partOwner = player.Name
	for c = 1, 30 do
		local p = Instance.new("Part")
		p.Name = partOwner
		p.Parent = game.Workspace
		wait(0.5)
	end
end

If another “Player2” calls the same ServerScript function and start spawning his own Parts, AND the “Player1” Parts are still spawning one by one…
1 - The names of the parts dont get mixed?..
I mean, its like 2 different threads?

2 - If more Players, are clicking that function too, is there a chance Parts get wrong names?

I tested it on Studio, by using 4 players, and it worked… But I kinda feel it will get errors while using in real server with real players.

3 - If I change this ServerScript into a Module is there an improvement?

4 - If the Module is called by a LocalScript, the Parts will only exist in LocalSide?

Thank you for ur time :3

1 Like

No, there is no way your names can get mismatched. The reason is that each function runs individually, and that your partOwner variable’s scope is to just that function( the “local” before your variable definition ). Basically, each function has its own part owner variable, to be specifically used only inside of that function.

Since your code is so simple, using a ModuleScript isn’t really necessary. ModuleScripts don’t really improve performance, but just group things and make things easier in some instances.

And yes, if you called the ModuleScript from a LocalScript, the code in the module would be run locally, meaning only the client that called it could see the parts, or any results of them.

2 Likes

Yeah it’s not the server script function that’s causing these threads. It’s actually the function being bundled within the click detector connection event which was connected.

Every event connection is equal to a new thread if I’m not mistaken. Kinda like a new script everytime a player clicks it that’s how I would think of it. So yeah like what @ExcessEnergy said the names won’t get mixed because it’s all local within these threads for each player that clicked it.

Edit: nice going with the experimentation though that is one of the “best” way to learn :+1:

there are others as people learn differently but thats a different topic for a different time

2 Likes

I see, thank you so much @ExcessEnergy
So everything is fine! thats great.
My idea of turning it into a module its because this is not the real script, just a quick example to make questions easier. (I hate when someone paste a very long code that makes no sense while asking something simple xD)

Thank you @dthecoolest! BTW, you mention the connection with click detector, but Im using a GUI button, there’s no difference at all right?
(I love to experiment! Thank you, I hope everything works fused together at the end)

1 Like

Yeah, the only difference is when and why the event fires/runs/executes as well a Gui button has its own events mouse button one and activated since it’s a GUI and not a part.

1 Like

Excelent! Thank you so much @dthecoolest!! Then lets keep working on! :3