Performance question related to script(s) & what they can do

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 !

Which situation would you use for better performances?
  • Situation 1
  • Situation 2
  • Situation 3

0 voters

Thanks for having read !
Varonex_0

Less scripts here is better, but I don’t recommend using folders to identify objects with unique behaviors. In most cases, you should use CollectionService for security as well as a better workflow.

2 Likes

I already thought about using CollectionService already.
I did not state what I wanted to do because it’s far more complicated, and I couldn’t even do it with parenting so I must use CollectionService anyway! But thanks for stating it here!

1 Like

Technically, option 3 is going to be the most performant. However, the difference between the first and third option are probably unnoticeable. I’ve seen obbies that have kill brick scripts in every individual kill brick part, and they work fine.

The reason you should use option 3, is so you only have one script. Future changes can be easy and you don’t have to delete 45 scripts and reimplement them. I also usually use folders for ease of access for both myself and builders.

1 Like

I would definitely recommend option 3 as having fewer scripts to edit when something needs to be changed is less of a hassle.

I also agree with other replies of use of CollectionService. It is a pretty easy and simple (Most of the time, simple == best solution) way to deal with a large amount parts that need to do the same thing.

Also if you are up to the challenge, you could, you know, create a class called “Door” and have a function to create objects that can control the door… (Don’t thought, this is not really needed) … unless?

1 Like

I still have a question about CollectionService:

  • Is there a possibility to see which tags were added, and even more, which instances are tagged with the latter?

  • If there isn’t such UIs in default RBOLOX, can a plugin do this?

It is quite hard to visualize which parts have which tag, that’s why I’m sometimes a bit skeptical to use this service: I don’t want to lose trace of work!

(The plugin can be made by myself it isn’t a problem, I’d only need to get a method to retrieve which tags exist in the current place)

1 Like

Performance change will be negligible for what you’re trying to do, but it’s easier to maintain, fix, and update one script than to do so for 45 scripts. I would go with situation 3.

1 Like

There is a plugin for this!

This devforum post by the creator of the plugin links the plugin, details how to use it, and provides more info about CollectionService:

2 Likes

3 is recommended but what i choose is to use Tag Editor along with CollectionService so you don’t have to use for loops and you can easily manage them without having to worry with something else.

You don’t need to install that plugin anyway, Roblox added this as a Official tool in studio recently and functions similarly to this plugin.

1 Like

I didn’t notice it was added, and it is really recent !

Thanks for all your help, I was pretty sure about the outcome, but still wanted to open this post.

What I am a bit struggling to find documentations on (I’m not the best at searching things lol) is any performance analysis tools.

I know:

  • the dev console highlights and profilers.
  • Shift + [key] windows for physics et cetera
    …

But I somehow would like to have more control over performances analysis. Are there more tools about this you are aware of?