Hello dear developers !
Here is the situation:
Alright so, imagine a server in which you need to make doors. Their script will be translated into:
function ToggleDoor()
--code of the door
end
I am not asking help about the script but about how I could manage these doors. 3 situations:
Situation 1
I have 45 doors within my map. They are all in a folder called Doors
whose parent is the workspace
directly.
Each of these doors has a script containing the following:
--SCRIPT PARENTED TO THE DOOR MODEL
local State: boolean, debounce: boolean = false, false
function ToggleDoor()
--code of the door
end
script.Parent.Door.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
State = not State
ToggleDoor()
task.wait(1)
debounce = false
end
end)
As said, the program itself isn’t important, hence the fact I didn’t write it down. It is only an example!
It means you have 45 scripts running the same piece of code. Modifying it is kind of a nightmare.
Situation 2
Same configuration as Situation 1, but now, I decided to add a ModuleScript
in ServerScriptService
.
I now have 45 script, each parented to a door, and one module in SSS:
--SCRIPT PARENTED TO THE DOOR MODEL
local State: boolean, debounce: boolean = false, false
local module = require(game.ServerScriptService.ModuleScript)
script.Parent.Door.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
State = not State
module:ToggleDoor(script.Parent, State)
task.wait(1)
debounce = false
end
end)
--MODULESCRIPT IN SSS
local module = {}
function module:ToggleDoor(DoorModel: Model, State: boolean)
--code of the door
end
return module
Situation 3
I have my 45 doors, all parented into a folder called Doors
, itselfparented in workspace
. I only use 1 script to manage them all !
I have 1 script in ServerScriptService:
--SCRIPT IN SERVERSCRIPTSERVICE
function ToggleDoor(DoorModel: Model, State: boolean)
--code of the door
end
for _, DoorModel: Model in pairs(workspace.Doors:GetChildren()) do
local State: boolean, debounce: boolean = false, false
DoorModel.Door.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
State = not State
ToggleDoor(DoorModel, State)
task.wait(1)
debounce = false
end
end)
end
We may try to combine this with an actor, to desync the code.
OUT OF ALL THESE SITUATIONS
Which one would you use? In terms of performances ?
Let me know by commenting & voting using this poll !
- Situation 1
- Situation 2
- Situation 3
0 voters
Thanks for having read !
Varonex_0