I’m not too well-versed in how modules work, but I thought I had a pretty good understanding of them. I use them so that instantiated objects in my game can be interacted with by more than just one script, which has worked so far for everything except tools.
My current setup is that each tool contains a regular script, a local script, and a module script that returns an instance of the tool’s class. The script and local script handle the equipped/unequipped events. If I try requiring the module from an external script, I get an object that doesn’t seem to reflect the current state the object is in; .isEquipped
property is false when it’s true in the tool’s script.
So to debug, I put another script in there that requires the module and prints it to the output every few seconds, and suddenly it acts as intended and requiring it doesn’t return the wrong object from an external script.
Class Module
--[[ Variables ]]--
-- Services --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Assets --
local assets = ReplicatedStorage:WaitForChild("Assets");
local animationAssets = assets:WaitForChild("Animations");
-- Modules --
local modules = ReplicatedStorage:WaitForChild("Modules");
local EquippableGun = require(modules:WaitForChild("EquippableGun"));
--[[ Equippable ]]--
return EquippableGun.new(script.Parent);
Server/Client Tool Handler
--[[ Variables ]]--
-- Services --
local ReplicatedStorage = game:GetService("ReplicatedStorage");
-- Tool --
local tool = script.Parent;
local equippableObject = require(tool.EquippableClass);
--[[ Functions ]]--
function onEquipped()
equippableObject:equipTo(tool.Parent);
end
tool.Equipped:Connect(onEquipped);
function onDequipped()
if (equippableObject.isEquipped) then
equippableObject:dequip();
end
end
tool.Unequipped:Connect(onDequipped);
function onDestroyed()
if (equippableObject.isEquipped) then
equippableObject:dequip();
end
end
tool.Destroying:Connect(onDestroyed);
Debug Script
local x = require(script.Parent.EquippableClass);
while (task.wait(1)) do
print(x);
end