I am currently making an OOP data handling module for a DBZ project I’m working on. Everything is working as intended, but one thing has been bugging me for quite some time now.
Whenever I use this function to get a data object, the script thinks it doesn’t exist. Don’t get me wrong, the module & the server script both work as intended, it is just that this issue could make my life 27.42% more tedious
function datahandler:GetCombatData()
if self.CombatData then
return self.CombatData
else
print("Combat data not found for "..self.Owner.Name)
return nil
end
end
Here is the code for the constructor:
function datahandler:CreateCombatData()
if not self.CombatData then
local char = nil
if self.Owner:IsA("Model") then
char = self.Owner
elseif self.Owner:IsA("Player") then
char = self.Owner.Character
end
self.CombatData = setmetatable({},combatDataMeta)
self.CombatData.Character = char
self.CombatData.Attacking = false
self.CombatData.Ki = 100
self.CombatData.MaxKi = 100
self.CombatData.Stunned = false
self.CombatData.Ragdolled = false
self.CombatData.Powerlevel = 10
self.CombatData.CurrentPowerlevel = 10
self.CombatData.Transformation = "Base"
self.CombatData.Limbs = {--[[Come back to this pls]]}
print("Created combat data for "..self.Owner.Name)
else
print("Combat data already exists for "..self.Owner.Name)
end
end
I get that this is a very niche issue, but it has been bugging me for quite a while (My memory is horrible lol). Also, if you have any ideas on how to make this system better, please tell me in replies. Any help/feedback is appreciated!
Could be maybe that the script doesn’t know what self.CombatData is so the script thinks it will return nil because you are returning nil if there is no CombatData. If you were to remove the return nil, it will probably only say “a”. But I guess it will return CombatData since you provide it later on
Some pretty rad techniques to overwrite Roblox’s type checking is to do custom type annotations. So you can create a custom type in a few ways, which all allow you to apply said type to a variable and auto complete should work it’s wonders.
Creating types:
type TypeNameHere = typeof(Class.new()) -- pretend class.new returns an object
type TypeNameHere = {
SomeKey : boolean,
AnotherKey : number, -- // etc...
}
-- // I recommend this one
-- // export allows you to require a module with
-- // this type in it, and use it outside the module
-- // like doing || local var : module.TypeNameHere
export type TypeNameHere = {} -- // whichever method you want to create said type
Applying type checking:
-- // number is the type
local variable_name : number = 123
-- // or like this
local variable_name = 123 :: number
-- // some fancy fancy stuff like
local function foo(this_param : number, that_param : boolean)
end
-- // applying custom type
type Banana = {
IsFruit : boolean,
Color : Color3,
CanEat : boolean,
SomeMethod : (param : number, param2 : boolean) -> any,
}
local bananaObj = BananaClass.new() :: Banana
-- // pretend banana class is in it's own module
-- // with a export type Banana declared there
local bananaObj = BananaClass.new() :: BananaClass.Banana
Obviously not a full guide for type annotations but I hope it guides you into what to look up. And most importantly, hopefully this removes your 27.42% tedious work!