Optimization Question

Is there any tips for optimization you could give, since I feel like that the word is being thrown around loosely sometimes. Would you say a script needs to be optimized if it went something like this for ex,

local plr

--something is an example no real variable
local function DoSomething()
--does the thing-
task.wait(10)

DoSomethingElse(plr)
else 

function DoSomethingElse(plr)
--checks for something and alot of other things-- 
if "something else" then
DoSomething()

end
DoSomethingElse(plr)

Let’s say there was a bunch of players or something who are running this same scrpt and there comes like a bunch of DoSomethingElse() at the same time, if maybe their cooldowns all end at once wouldn’t that lag , I have tried to run many functions with multiple loops at once and it does lag, so what would the optimization for that be?

thanks.

Looking at the code, this would only run in a local script. So it doesn’t matter how many players run it, because the script will run on their computer only.

1 Like

Why do you need a recursive function? Just use a while loop :exploding_head:

This doesn’t seem like an optimization question, more of a code architecture one. There’s not a lot more than can be meaningfully said about such an abstract and hypothetical question. My recommendation would be to keep coding your actual game, and come back to this if you have a concrete example that’s a solvable problem. Maybe the right answer is a loop, or a queue of events that’s consumed on a particular RunService event, or maybe your real problem well suggest using coroutines or Actors. Impossible to say without knowing the real game mechanic you’re making, and how it’s not working out.

local X =

function DoSomething()
    --does the thing-
    task.wait(10)
end
function DoSomethingElse(plr)
    --checks for something and alot of other things--
   if "something else" then
     DoSomething()
   end
end

if
  DoSomething()
else 
  DoSomethingElse(plr)
end

Variables defined then Functions defined, then logic.
Calls from function to function, have the function being
called on top of the calling function.