Hello, thanks for taking the time to read my post.
- What do I want to achieve?
I would like to know either what it is that is wrong with my syntax when attempting to create a class structure for a game I am creating, or whether or not what I am attempting to do is even possible.
- What is the issue?
When creating a subclass, I have no trouble at all inheriting both properties and methods in the main class. However, when I want to create a new method specific to a subclass, I cannot for the life of me figure out how to actually use it in the context of “local newClass = MainClass:New()”
In the example below, in the main ServerScript, the method that is inherited from the BaseClass works, but the newly created :FIre() method does not, and gives the error “Attempt to call a nil value”, which I assume means that the pistolClass:New() constructor does not contain the newly created :Fire method, and I am unsure how to fix it.
I just created this easier to read example to showcase the issue.
-- Example main "GunClass"
-- Services:
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Create base Gun Class
local GunClass = {}
GunClass.__index = GunClass
function GunClass:New()
local self = setmetatable({}, self)
self.FireRate = 1
self.BaseDamage = 15
self.TestPropety = "Nooo"
self.BeastMode = true
return self
end
function GunClass:BaseFire()
print(self.BaseDamage)
end
return GunClass
Really nothing special.
-- Example pistol class which inherits the properties and methods, but attempts to create a custom method as well.
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
-- Remote Event Module
local RemoteEventModule = require(ReplicatedStorage:WaitForChild("RemoteEventModule"))
-- Base Gun Class
local GunClass = require(script.Parent)
-- Every Pistol Bullet Option
local DefaultBulletClass = require(script.Parent.Parent:WaitForChild("BulletClass"))
local Tier1PistolBullet = require(script:WaitForChild("Tier1PistolBullet"))
-- The Module
local PistolClass = {}
setmetatable(PistolClass, {__index = GunClass})
-- Table of functions for each bullet type
PistolClass.BulletTypes = {
--[DefaultBulletClass] = Tier1PistolBullet;
["Tier1PistolBullet"] = function()
return Tier1PistolBullet:New()
end,
}
-- Create default stats that the bullet will inherit (and edit)
function PistolClass:New()
local self = GunClass:New()
self.FireRate = 1
self.BaseDamage = 20
warn("Created new Pistol Var")
return self
end
function PistolClass:Fire(bullet)
local bullet = PistolClass.BulletTypes[bullet]()
-- Fire the gun hopefully
bullet:Fire()
end
return PistolClass
And lastly, the ServerScript I have been using to test this:
local ServerScriptService = game:GetService("ServerScriptService")
-- classes
local GunClass = require(ServerScriptService.WeaponClasses:WaitForChild("GunClass"))
local pistolClass = require(ServerScriptService.WeaponClasses.GunClass:WaitForChild("PistolClass"))
task.wait(1)
local Pistol = pistolClass:New()
Pistol:BaseFire() -- works
Pistol:Fire() -- does not work
- What solutions have I tried thus far?
Basically just attempting different ways to write each of the classes. I will not pretend like I am an expert on the subject, but I did as much research as I could, trying various ways, and I am unsure how to get this pretty basic-in-theory idea to work.
Also, if anyone has good resources on OOP that are easily applied to LUAU, I would appreciate if you shared them. I have pretty much seen every YouTube tutorial that relates to Roblox, as well as read a majority of the devforum posts.
thanks again,