I apologize in advance if this doesn’t belong in “Scripting Support”.
I’m trying to pick Roblox Studio up and mess around. I’ve been around the documentation and the forum but I have a fairly basic question I can’t seem to formulate it so I find my answer.
Say I have an “object” such as a Killbrick, which would be made of a basic Part containing a script bla-bla-bla…
First of all: Where does this belong under game ? What if I wanted to create more “Killbricks” based on this one, programmatically? With a line such as local killbrick = Instance.new("Killbrick")? Is there even a way to make such a line work? (Extending Instance so it recognizes Killbrick as a key-word… idk) If not, how?
I apologize if my question is not clear: I’m asking from a code design POV, what’s the intended way - the best practice if you will - to work on independent objects so you can access them later, duplicate them, etc?
Thanks in advance, and don’t hesitate to ask for more information if I’m not clear enough
You should look into modules, and thus OOP systems. You’re new, so it might take some time to wrap your head around, but it’s worth it for reusable systems of customizable objects. OOP stands for “Object Oriented Programming”…
Here’s a tutorial on it (the one I used when first learning).
I believe this solves your problem. Let me make an example, I will create a Killbrick “Class”
-- // module code
local killbrickModule = {}
killbrickModule.__index = killbrickModule
function killbrickModule.new(cframe, size, damage, enabled) -- pass information (customizable)
local self = setmetatable({}, killbrickModule)
local brick = Instance.new("Part")
brick.Anchored = true
brick.CFrame = cframe
brick.Size = size
brick.Parent = workspace
self.Killbrick = brick
self.Damage = damage
self.Enabled = enabled
self:Initialize()
return self
end
function killbrickModule:Initialize()
self.Killbrick.Touched:Connect(function(hit)
if not self.Enabled then return end -- access objects in other methods using
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(self.Damage) -- access objects in other methods using "self"
end
end)
end
return killbrickModule
From another script:
local killbrickModule = require(path.to.module)
local myBrick = killbrickModule.new(CFrame.new(10, 0, 0), Vector3.new(10, 1, 2), 3, true)
local mySecondBrick = killbrickModule.new(CFrame.new(10, 0, 5), Vector3.new(10, 1, 2), 5, true)
And you can add LOADS of more functionality. This is an oversimplified example.
This would be Object Oriented Programming Tho there is a possibility theres more ways of doing this but I found OOP being the most common way from my experience
Thank you very much for your answer and redirection to OOP. Just as a heads-up, I’m only new to Roblox, not software engineering as a whole (that’s actually my day job lol).
I wasn’t sure, before asking, if OOP was a great fit on Roblox (still not sure) but your answer convinced me to give it a try. Thanks a lot for the resources and the code snippet, I appreciate it a lot.
You can think of game as an entry point to the different directories in your Studio’s explorer. It is all just encapsulated into a DataModel object. The killbrick can belong anywhere you need it to depending on the context it will be used in. If it’s a part for reference that you will be cloning later, you can store the killbrick in the ReplicatedStorage or ServerStorage for cloning with :Clone().
As for extending Instance, look into inheritance. Just remember that Lua is fundamentally a functional language so any OOP practices here may not entirely apply to other languages like C# if you were to go to Unity, although there are huge similarities between metatables and virtual tables in other languages
It is. Definitely. People love OOP and use it all the time, I use it too. Even in the default ControlModule and CameraModules they use it. Roblox uses this themselves. It’s recommended