Equip is fired before Unequip finished running

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)

I would like to add that none of the functions you see after self (for example playAnimation) have task.wait()

Invoking the server waits until the client recieves a result, that’s probably where your issue is coming from

I tried writing RS.Unequip:InvokeServer() at the end of Unequip method but that didn’t work

you could try task.spawning it but there’s probably a better way to do it than that

1 Like

Wrapping RS.Unequip:InvokeServer() in task.spawn seems to work, however when I switch the tools too fast it eventually breaks

Well I guess disabling player’s backpack as a cooldown after the item has been equipped can only prevent this

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.