Tool works even when unequipped?

So I am making an animation for when the player clicks while the tool is equipped and an animation would play pretending they are drinking the drink.

The problem is that it plays even when the tool isn’t equipped.

This is how IT SHOULD look like:

This is how it is RIGHT NOW-

Code is a local script

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7360460401"

local drinkanim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)

local clicks = 0
local db = false
local drinkSound = script.Parent.Handle.DrinkSound

print("Ready!")

script.Parent.Equipped:Connect(function()
	local mouse = game.Players.LocalPlayer:GetMouse()

	mouse.Button1Down:Connect(function()
		if clicks == 0 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 1
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 1 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 2
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 2 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 3
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 3 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 4
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 4 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 5
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 5 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 6
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 6 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				wait(2)
				clicks = 7
				print(clicks)
				db = false
			end
		end
	end)

	mouse.Button1Down:Connect(function()
		if clicks == 7 then
			script.Parent:Destroy()
		end
	end)
end)

Can anyone help?

Make a actived value, when you equip the tool, that value sets to true, and when you unequip the tool, the value gets to false.

Changing

Script.Parent.Equipped to Script.Parent.Activated should work if I’m not mistaken.

I already tried that, doesn’t work.

Something like this?

local activated = false

print("Ready!")

script.Parent.Equipped:Connect(function()
	activated = true
	local mouse = game.Players.LocalPlayer:GetMouse()
	
	if script.Parent.Unequipped == true then
		activated = false
	end

ah alright, I just took a quick glance at the script and video so I didn’t think it would work anyway.

Yes, thats what i said, and now you should stop the animation and the sound when you unequip the tool.

Oddly enough, it still plays even if the value is false-

local activated = false

print("Ready!")

script.Parent.Equipped:Connect(function()
	activated = true
	local mouse = game.Players.LocalPlayer:GetMouse()

	if script.Parent.Unequipped == true then
		activated = false
	end

	if activated == true then
...

You didn’t put the Animation parent.

1 Like

The same script that i replied, but fixed:

local animation = Instance.new("Animation", script.Parent)
animation.AnimationId = "rbxassetid://7360460401"
----
local drinkanim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
----
local drinkSound = script.Parent.Handle.DrinkSound
----
local drinks = 0
local db = false
local active = false
----

script.Parent.Equipped:Connect(function()
	active = true
	script.Parent.Activated:Connect(function()
		if active == true and db == false then
			db = true
			drinkSound:Play()
			drinkanim:Play()
			drinks += 1
			wait(2)
			if db == true and drinks < 7 then
				db = false
			elseif db == true and drinks == 7 then
				script.Parent:Destroy()
			end
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	active = false
	drinkanim:Stop()
	drinkSound:Stop()
	if db == true then
		db = false
	end
end)
1 Like

The reason why is because you have sooo many unnecessary listeners that always exist when you initially equip the weapon and thus keeps listening for the event and executes code in its scope, even when it’s unequipped. Here’s how it can be solved:

The Activated event fires when the player clicks with their tool equipped. Don’t nest connections if you’re not going to disconnect them: this leads to multiple listeners doing the same thing multiple times. Your code can just be simplified to this:

script.Parent.Activated:Connect(function()
    if db then return end
    
    if clicks >= 7 then
        script.Parent:Destroy()
    else
        drinkanim:Play()
		drinkSound:Play()
        clicks += 1
    end
    wait(2)
    db = false
end)
1 Like

It appears that the animation you’re using is still playing even after you unequip the tool, most likely because you forgot to stop it when the tool is unequipped. Also, binding 7 events to a MouseClick isn’t the best practice. Try this:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7360460401"

local tool = script.Parent
local drinkanim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)

local connection
local clicks = 0
local db = false
local drinkSound = script.Parent.Handle.DrinkSound

print("Ready!")

script.Parent.Equipped:Connect(function()
	connection = tool.Activated:Connect(function()
		if clicks < 7 then
			if db == false then
				db = true
				drinkanim:Play()
				drinkSound:Play()
				task.wait(drinkanim.Length)
				clicks += 1
				print(clicks)
				db = false
			end
		end
	else
		script.Parent:Deactivate()
		script.Parent:Destroy()
	end)
end)

script.Parent.Unequipped:Connect(function()
	if connection then
		connection:Disconnect()
	end
	if drinkanim.IsPlaying then
		drinkanim:Stop()
	end
end)

Let me know if this doesn’t work.

You didnt put animation parent too, if im right the anim should have a parent.

I allways use tool.Activated when I make tools with animations.

The reason is probably because you didn’t disconnect those Mouse events.

1 Like

Actually, that’s not required. You can load animations even if they don’t have a parent.

1 Like

Script works correctly and the animation doesn’t play when the tool is not equipped! Thanks for the help!

1 Like