Need some help with tools unequip

So, i have a tool and i need to make it that when a value is false then the player can equip it but if not then the tool will just get unequipped

this is my code, if someone can help me in a way that i will learn it will be helpfull

local cooldown = 1.25
local debounce = false

local player = game.Players.LocalPlayer

local IsFlying = player:WaitForChild("Is").IsFlying

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if debounce == false then 
			debounce = true
			local PunchingAnimation = player.Character.Humanoid:LoadAnimation(script.Parent.PunchingAnimation)
			PunchingAnimation:Play()
			wait(cooldown)
			debounce = false
		end
	end)
end)

Please help me learn from this, Thank you for reading.

you could use the Humanoid:UnequipTools function which makes any tool which the player is holding to get unequipped

but use a if else statement and then add the function in it

local cooldown = 1.25
local debounce = false

local player = game.Players.LocalPlayer

local IsFlying = player:WaitForChild("Is").IsFlying

script.Parent.Equipped:Connect(function(Mouse)
	if IsFlying.Value == false then
		Mouse.Button1Down:Connect(function()
			if debounce == false then 
				debounce = true
				local PunchingAnimation = player.Character.Humanoid:LoadAnimation(script.Parent.PunchingAnimation)
				PunchingAnimation:Play()
				wait(cooldown)
				debounce = false
			end
		end)
	else
		player.Character.Humanoid:UnequipTools()
	end
end)

So, when i tried to make the value of “IsFlying” to true im getting this warning, and the tool dont get unequiped…
this is the warning:
Something unexpectedly tried to set the parent of to Backpack while trying to set the parent of . Current parent is GalPlayss.
Can you help me with that maybe? :stuck_out_tongue:

You could use another function called Unequipped to unequip if this is what you’re looking for as it can print some text or a variable, and especially stopping the animation in the tool as it shows when you Equipped the function for the tool. Here’s an example of what using Unequipped would do:

local Tool = script.Parent

Tool.Equipped:Connect(function()
    print("Equipped was successful!")
end)

Tool.Unequipped:Connect(function()
    print("Unequipped was successful!")
end)

Using this instead of having another function inside of the Equipped function would be beneficial for you and others looking for a similar solution as it can detect when the tool is away or visible to you and players. You should put this at the bottom or after the Equipped function as it will be kind of confusing to you and the script if I recall.

Would recommend looking at this: Unequipped.

2 Likes