Getting used to Roblox's paradigm

Hello! For the past month, I have been trying to get back into Roblox Studio after about a year of using other engines like Godot and Unreal Engine, both of which use object-oriented languages. The problem is that I have gotten so used to OOP that I now find Roblox’s luau very unintuitive. I find it easier to write tidy and good code using OOP rather than using Luau, where I feel like I have to find roundabout solutions each time. I know that there are things such as metatables that are meant to recreate OOP’s behaviour but I find them unintuitive to use, especially since functions cannot be called from outside the script like they can be in Godot and Unreal Engine. I’ve heard this can be solved using Bindable Events but I’ve heard their use is frowned upon. Module scripts also seem like a solution but I am not sure how often and when they are used and I don’t want to overuse them or use them wrongly.

Any help and tips with my problem would be appreciated!

What do you mean “functions cannot be called outside the script”? Seperate scripts can require the same module, maybe there could be a container within the module? I don’t really get what you mean.

I dont tend to find metatables that much different to Object-Oriented languages, you just need to contain your methods and add another one to create the object. Can you explain a bit more what you’re struggling with about metatables?

For your first question, let’s say I have an animal. The player then damages the animal. What I would’ve done previously is that the player attack script would calculate the damage and then change the animal’s humanoid’s health.

However, that would be very inefficient, since each thing that is meant to damage the animal would have to have the piece of code responsible for damage in them, creating a lot of repetition, not to mention how complicated things would get if there was a different type of animal that calculated damage differently.

What I would do now is that when the player attacks the animal, the player attack script would run some kind of damage function that is found inside the animal ai script, however I can’t do that with a normal script, the only ways I can think of is to use a module script, however, module scripts cannot itself run code, meaning that I cannot run AI.

Even if I do use a module script for each animal, I imagine that would be quite memory inefficient, considering I am cloning the script for each time a new animal spawns. The best solution to this would be classes, since I will be able to use the same functions for each animal without replicating them and using up memory.

This leads to your second question. I found that metatables can be used similar to classes when done like this:

-- inside a module script
local EntityClass= {}
EntityClass.__index = EntityClass

function EntityClass.new()
	local instance = setmetatable({},EntityClass)
	instance.health = 100	

	return instance
end

function EntityClass:Damage(damageAmount)
	self.health -= damageAmount
end

return EntityClass

An object of the EntityClass can then be created in another script like this:

local animalClass = require(module I showed above)

local animal = animalClass.new()
animal:Damage(20)

The problem is that I don’t know how I would make this work with the animal. When the player attack script is meant to attack the animal, I am not sure how I would get the animal object and then call the Damage function in a way that isn’t very clunky. Of course, keep in mind that I am not sure if this approach of using metatables for animals is even the best way out there.

Personally I use a module named InstanceToEntity which just returns a table then store the object/entity there.

Then I also automatically add a function to detect if the instance is destroyed to destroy the object and remove it as well.

Yep in Roblox its all module scripts for ease of script to script communication.

If you look at frameworks Knit or ECS its all module scripts.

I would recommend just using them. Since most module scripts are mostly just functions (function based programming?) you can easily copy paste and group them together to organize it.

You will inevitably have to refactor and even have to write documentation for your own game as it gets larger which is my current problem. I would just recommend being able to handle the scenario where you rename a module script or move it to another folder.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.