I’ve got into a problem with Equip method firing before Unequip finished running. It only happens when I switch the tools (for example I press 3 and then 6 lets say):
ModuleScript:
function ToolClass:Equip(Item : Tool)
print("Equipping...")
if not Item:FindFirstChild("Settings") then return warn("No settings found") end
self.Settings = require(Item:FindFirstChild("Settings"))
self.currentTool = Item
self.equipped = true
self.loadAnimations(self.Settings.animations,Player.Character.Humanoid.Animator)
self.updateGUI()
self.playAnimation("equip")
self.playAnimation("idle")
self.binding()
self.itemmodel = RS.Equip:InvokeServer(Item)
print("ToolClass:Equip() has finished")
end
function ToolClass:Unequip(Item : Tool)
print("Unequipping...")
self.stopAnimation("idle")
self.stopAnimation("equip")
self.animationTable = {}
RS.Unequip:InvokeServer(Item,self.itemmodel)
self.itemmodel = nil
self.Settings = nil
self.equipped = false
self.currentTool = nil
print("ToolClass:Unequip() has finished")
end
It’s going to print out in order: Unequipping… > Equipping… > ToolClass:Unequip() has finished > ToolClass:Equip() has finished
LocalScript:
local ToolSystem = require(game.ReplicatedStorage.Modules.ToolSystem).new()
local char = script.Parent
char.ChildAdded:Connect(function(inst)
if inst:IsA("Tool") then
ToolSystem:Equip(inst)
end
end)
char.ChildRemoved:Connect(function(inst)
if inst:IsA("Tool") then
ToolSystem:Unequip(inst)
end
end)
How can I make it that when player switches tools it fires Unequip method first, waits until it has fully finished running and then Equip method can fire? (I could add task.wait() on start of the Equip method but Im sure theres more effective solution to this)