This script works even if the tool is not equipped

So , i’m making a soda tool that you can drink but when i unequip the tool , it still plays the animation when i click. Any help would be appreciated!
LocalScript:

repeat wait() until game.Players.LocalPlayer.Character.Humanoid
local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local b
local deb = false
local equipped = false
local anim = char.Humanoid:LoadAnimation(script.Parent.Animation)
local timee = 2
script.Parent.Equipped:Connect(function()
	equipped = true
end)

script.Parent.Unequipped:Connect(function()
	anim:Stop()
	b:disconnect()
	equipped = false
end)

  b  = 	mouse.Button1Down:Connect(function()
		if deb == false then 
			if equipped == true then
				deb = true
				anim:Play()
				wait(timee)
				deb = false
			end
		end
	end)

Explorer (StarterPack) :
Screenshot_18

2 Likes

Here’s some code that works nicer

local Player = game:GetService('Players').LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Mouse = Player:GetMouse()
local anim = Character.Humanoid:LoadAnimation(script.Parent.Animation)

local ClickConnection
local Debounce = false

local timee = 2

script.Parent.Equipped:Connect(function()
	ClickConnection = Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
			anim:Play()
			wait(timee)
			Debounce = false
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	ClickConnection:Disconnect()
end)```

Why are you disconnecting the Button1Down function each time you unequip and making it each time you equip?

Why not use Tool.Activated instead? It seems like the easiest solution and if needed you can keep your Unequipped function to stop the animation, without having to deal with any Connection or equipped boolean.

1 Like

For whatever reason it fixed itself , it works now.

Things don’t just magically fix themselves…

You should consider bug testing more to make sure that its fixed.

The script is working with the tool unequipped because you’re needlessly grabbing the mouse and connecting outside the equipped event. The player’s mouse is automatically passed as an argument to Equipped, so if you encase your logic there then you won’t be receiving unintended results. On unequip, the mouse becomes “inactive” and the connections are removed/ In addition, you should really just use the Activated event for simple tools.

local Tool = whatever

Tool.Equipped:Connect(function (mouse)
    mouse.whatever
end)

Furthermore, two things. First: if you find a solution yourself, please elaborate on the steps you took until it finally worked. Second, if a response solved your inquiry, please mark it as the solution.

1 Like

Finally , i found a solution for this.
Basically , i looked into my other creations and found a script like this one but different. I edited the script and now it seems to work perfectly fine.
Basically instead of changing “equipped” to false/true after equipping/unequipping , it just checks what is the parent of the tool and if it is “char” (Character) when the player clicks Button1 on his mouse then it plays the animation.
Script:

wait(1)

local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
local anim = char.Humanoid:LoadAnimation(script.Parent.Animation)
local mouse = plr:GetMouse()
local deb = false
	if hum.Health == 0 then return end
	mouse.Button1Down:Connect(function()
		if script.Parent.Parent == char then
			if deb == false then
				deb = true
				anim:Play()
				wait(2.1)
				
				deb = false
			end
		end
	end)
3 Likes