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)
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
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
...
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)
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)
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)