(This is a bit shoehorned in, but I want to comment real quick on the technique you’re using to make a Sword module. This can solve the problem you’re having here, and also make your sword more robust.)
You’re on the right track, but there are some things you could be doing differently to make it easier on yourself. I’m about to dump a lot of info which I explained to @loleris the other day.
If you’re willing to hear me out, I think this might be the long-term solution you’re looking for
Lua has syntax sugar that allows you to create pseudo-classes from a table through the usage of:
- Metatables
- The
:
operator
- The
self
variable.
Here’s a functional example class that can be constructed:
local Class = {}
Class.__index = Class
function Class.new(a, b, c)
local inst =
{
A = a;
B = b;
C = c;
}
return setmetatable(inst, Class)
end
function Class:WhatIsA()
print("My value for A is", self.A)
end
function Class:WhatIsB()
print("My value for B is", self.B)
end
function Class:WhatIsC()
print("My value for C is", self.C)
end
function Class:LogAll()
self:WhatIsA()
self:WhatIsB()
self:WhatIsC()
end
function Class:GetValueSum()
return self.A + self.B + self.C
end
return Class
Using this code pattern, you can create modules that can have their functions overloaded by defining those functions on a new instance of that module.
In theory you could create a Sword
ModuleScript that allows swords to hook up their specific functionality. Here’s some potential pseudo-code without an actual implementation for the Sword
module:
local IceDagger = Sword.new
{
Tool = script.Parent;
SlashDamage = 10;
LungeDamage = 35;
}
function IceDagger:OnEquipped()
-- TODO
end
function IceDagger:OnUnequipped()
-- TODO
end
function IceDagger:OnActivated()
local now = tick()
if (now - self.LastClick) > 0.25 then
self:Lunge()
else
self:Slash()
end
end
function IceDagger:OnVictimHit(humanoid)
-- Logic for freezing the hit humanoid.
end
Through the magic of __index
, calling self:OnActivated()
in a function of the Sword
module would “downcast” to the functions declared in the IceDagger
script.
You could setup event handlers and other general logic shared by all of the swords in the Sword
module, while the overload functions defined in IceDagger
would handle the functionality specific to the Ice Dagger.
A great talk was done about this at RDC 2019.
Skip to 19:49 if you’re interested in the bit relevant to what I’m talking about here:
Let me know if you have questions!
Sorry in advance for the verbose posts lol.