Tool ability system not working as intended after unequipping

Hello,

javipachuflo_2 here.

Little fyi: I made italics what the message said by default. I thought it provided a bit of structure so I did not remove it.

1. What do you want to achieve? Keep it simple and clear!

I want to create a system where when a player holds out a tool, it gives them an ability and when they stop holding it, it removes the ability effects.

So far, I have this:
A script in ServerScriptService which creates two Remote Functions:
One to enable the ability and another to remove it.

Here is the script:

-- ServerScriptService (Server)
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local triggerAbilityFunction = Instance.new("RemoteFunction")
triggerAbilityFunction.Name = "TriggerAbility"
triggerAbilityFunction.Parent = ReplicatedStorage

local undoAbilityFunction = Instance.new("RemoteFunction")
undoAbilityFunction.Name = "UndoAbility"
undoAbilityFunction.Parent = ReplicatedStorage


function triggerAbility(player)
	
	local inHand = player.Character:FindFirstChildOfClass("Tool")
	local toolName = inHand.Name
	if inHand then
		if toolName == "Stick" then 
			print("Stick equipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 50

		elseif toolName == "Hammer" then
			print("Hammer equipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 50

		elseif toolName == "Viking Axe" then
			print("Viking Axe equipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 50

		elseif toolName == "Metal Bar" then
			print("Metal Bar equipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 50

		elseif toolName == "Log" then
			print("Log equipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 50
		end
	end
end

function undoAbility(player)
	local backpack = player.Backpack
	
	backpack.ChildAdded:Connect(function(unequippedTool)
		
		if unequippedTool then
			print(unequippedTool.Name)
		end
		
		if unequippedTool.Name == "Stick" then 
			print("Stick unequipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 16

		elseif unequippedTool.Name == "Hammer" then
			print("Hammer unequipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 16

		elseif unequippedTool.Name == "Viking Axe" then
			print("Viking Axe unequipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 16

		elseif unequippedTool.Name == "Metal Bar" then
			print("Metal Bar unequipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 16

		elseif unequippedTool.Name == "Log" then
			print("Log unequipped")
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
		end
	end)
end


triggerAbilityFunction.OnServerInvoke = triggerAbility
undoAbilityFunction.OnServerInvoke = undoAbility
	

The abilities of all of the tools are currently just a walkspeed increase as they are just a placeholder until I have it fixed.

The second script, a LocalScript, is placed within the Tool itself.
Here is what I have in the LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Get the RemoteFunction object from ReplicatedStorage that you created from the script in serverscriptstorage called 'RemoteFunctionTriggerAbility'
local triggerAbilityFunction = ReplicatedStorage:WaitForChild("TriggerAbility")
local undoAbilityFunction = ReplicatedStorage:WaitForChild("UndoAbility")

local equipped = false
local tool = script.Parent


tool.Equipped:Connect(function()
	equipped = true
	local success = triggerAbilityFunction:InvokeServer()
	if success then 
		print("ability activated")
		else
		print("failed to activate ability")
	end
end)

tool.Unequipped:Connect(function()
	equipped = false
	local success = undoAbilityFunction:InvokeServer()
	if success then 
		print("ability deactivated")
	else
		print("failed to deactivate ability")
	end
end)

Everything seems fine.
When I equip the tool, in this case called “Stick” (which is a little wooden cylindrical sword), the ability activates, regardless of the output printing “failed to activate ability” (if you know why this is printing when it is successful please say as well, I’d appreciate it).
My character’s walkspeed increases to 50.

Now to the issues

2. What is the issue? Include screenshots / videos if possible!

When the tool is unequipped for the first time, the ability is not disabled. After the second time, it disables once. After the third it disables twice and so on.

Let me show you with a video:
–Video

In the video, I show how the tool works but at the same time doesn’t.
.

Here is another one to show where the scripts are at:
–Video

Hopefully this will maybe explain why something is wrong?

3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried a few things to fix it like trying to change the final function to only be one but it would always result in error. I don’t really know what I was doing wrong. (I would add output screenshot but I deleted what I had done previously, sorry for the inconveniences)

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Please, if you need anything else, do not hesitate to ask!
I can provide with any more info you might need

This is my first post so if I did anything wrong, I’m sorry.

Thank you in advance!,
javipachuflo_2

In your undo function, you bind an event which will listen for children to be added to the backpack AFTER the tool is already moved to the backpack

The effects aren’t going away because the event doesn’t trigger if the tool already was moved to backpack, however on the second unequip the event does trigger (and starts listening a second time, oops!)

You should remove the event on line 45 of RemoteFunctionTriggerAbility and instead just pass the toolName with the event on unequip

Like on line 22 of “Ability”:
undoAbilityFunction:InvokeServer(tool.Name)

Which will let you do this on Line 42 of RemoteFunctionTriggerAbility:
function undoAbility(player, unequippedToolName)

1 Like

Thank you SO much Max, I really appreciate it.
I realise where I went wrong now.

I am not very good with parameters, specially when you need to add 2, because sometimes when you print them to understand what they are doing they just say “nil” or “Instance” which, to me, doesn’t make much sense.

But I understand it now. This problem at least.

After adding the Ability LocalScript to all of the tools, I can safely say they are all working as expected, look:
–Video

Thanks again and I’m sorry if I might put on too much text. I thought it was necessary.
Have a good day!

PS: The Log too was anchored AND had massless unchecked.

1 Like

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