Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can/should I use scripts as classes?

Asked by 6 years ago
Edited 6 years ago

So I was reading this guide: http://howtomakeanrpg.com/a/classes-in-lua.html And it showed off a fancy way of turning a table into a class, but why not just use a script as a class and add all the methods and variables for Orc to that?

name = "Orc"
maxHealth = 10;
currentHealth = maxHealth;
damage = 2;
--------------------------------------------------------------------------------
function TakeDamage(amount)


end

function Attack(mob)
    mob.takeDamage();
end
--------------------------------------------------------------------------------
function Start()

end

function Update()

end
--------------------------------------------------------------------------------
Start();
while(wait(1/60))do
    Update();
end
0
Keep in mind the built-in wait function allows you to yield the minimum of 1/30th of a second. For draw calls that need to be done before a frame is drawn, use the RenderStepped event. For other update calls use the Stepped or Heartbeat events as they are called after the frame is drawn. All are tied to the machine's framerate, often 1/60th of a second. ozzyDrive 670 — 6y
0
Please make use of the conditional portion of the while loop. User#24403 69 — 6y
0
Lol, yeah, I tested the Update method with print, and noticed that it was printing way slower than it should have done, but 30 fps is still fine I guess. bludud1234 40 — 6y

1 answer

Log in to vote
3
Answered by
Link150 1355 Badge of Merit Moderation Voter
6 years ago

Classes are meant to be templates or "blueprints" for objects. You can "instantiate" a class to create a new object from that class, called an "instance".

The problem with using scripts like you do is you can't create multiple instances of them.

You could say you can copy them, but not create a new instance.

Though I suppose you could keep an original in ServerStorage or ReplicatedStorage and copy that.

Using scripts as classes isn't actually a bad idea, but copying, reparenting, or enabling/disabling scripts programmatically should be avoided.

0
In fact, copying, reparenting, or toggling scripts programmatically is really a thing of the past and shouldn't be done at all. Link150 1355 — 6y
Ad

Answer this question