Hello!
I have nearly finished a simple soda that the player will can use. When the soda Tool is equipped, it plays an animation. When the player clicks, the player “drinks” the content. Everything works fine. But…
I have seen that the player will be able to glitch himself with the drink animation. I use Animation.Stopped:Wait() to make the soda more realistic and play the different animations correctly.
Here is the Local script:
local CAN = script.Parent
local COOLDOWN = false
local DRINK_REMAINING = 5
CAN.Equipped:Connect(function(Mouse)
local ANIMATION = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.HOLD)
local ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK)
ANIMATION:Play()
Mouse.Button1Down:Connect(function()
if COOLDOWN == false then
if DRINK_REMAINING > 0 then
COOLDOWN = true
ANIMATION:Stop()
ANIMATION_DRINK:Play()
script.Parent.Can.DRINK:Play()
ANIMATION_DRINK.Stopped:Wait()
ANIMATION:Play()
DRINK_REMAINING = DRINK_REMAINING - 1
if DRINK_REMAINING == 0 then
ANIMATION:Stop()
ANIMATION_DRINK:Stop()
CAN:Destroy()
end
COOLDOWN = false
end
end
end)
CAN.Unequipped:Connect(function()
ANIMATION:Stop()
ANIMATION_DRINK:Stop()
end)
end)
So, in the end, the only issue I saw is that the player will be able to unequip the tool while the drink animation is playing, and he will glitch himself with the animation because the script didn’t detect that he unequipped the tool, because the script is waiting for the drink animation to stop (I guess).
Is there any way to avoid that, please ?
Thank you!
Do you mean, if I put ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK) as a global variable like DRINK_REMAINING, etc ?
I tried to use what you said, so the script looked like this:
local CAN = script.Parent
local COOLDOWN = false
local DRINK_REMAINING = 5
local ANIMATION_DRINK
local ANIMATION_HOLD
CAN.Equipped:Connect(function(Mouse)
ANIMATION_HOLD = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.HOLD)
ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK)
ANIMATION_HOLD:Play()
Mouse.Button1Down:Connect(function()
if COOLDOWN == false then
if DRINK_REMAINING > 0 then
COOLDOWN = true
ANIMATION_HOLD:Stop()
ANIMATION_DRINK:Play()
script.Parent.Can.DRINK:Play()
ANIMATION_DRINK.Stopped:Wait()
ANIMATION_HOLD:Play()
DRINK_REMAINING = DRINK_REMAINING - 1
if DRINK_REMAINING == 0 then
ANIMATION_HOLD:Stop()
ANIMATION_DRINK:Stop()
CAN:Destroy()
end
COOLDOWN = false
end
end
end)
end)
CAN.Unequipped:Connect(function()
if ANIMATION_HOLD then
ANIMATION_HOLD:Stop()
end
if ANIMATION_DRINK then
ANIMATION_DRINK:Stop()
end
end)
But the animation doesn’t seem to stop at all. Even the Hold one.
Alright. I figured it out.
I finally fixed the issue.
Solution:
I removed the “local” of ANIMATION_HOLD = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.HOLD) and ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK), making them global and usable in the Unequipped function. (I removed local ANIMATION_DRINK and local ANIMATION_HOLD at the beginning.)
After that, I removed the line calling the ANIMATION_HOLD to stop. So I removed the linked line, calling play for this animation after the user played the drink animation.