Just wondering if someone could help improve my dance script? It’s buggy and won’t let you stop it without spamming the J button and after activating it once and stopping you have to spam the J key to get it to work again. It’s just very buggy and would love if someone could help fix it.
Here is the script:
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
local Animation = script:WaitForChild("Dance")
local UIS = game:GetService("UserInputService")
local debounce = false
local LoadedAnimation -- Global variable to store the current loaded animation
UIS.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.J then
if debounce then return end
debounce = true
if LoadedAnimation and LoadedAnimation.IsPlaying then -- Check if the animation is loaded and playing
LoadedAnimation:Stop() -- Stop the animation if it's already playing
else
LoadedAnimation = Animator:LoadAnimation(Animation) -- Load the animation
LoadedAnimation:Play() -- Start the animation if it's not playing
end
wait(10)
debounce = false
end
end)
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() -- Fires when the player tries to move
if LoadedAnimation and LoadedAnimation.IsPlaying then -- Check if the animation is loaded and playing
LoadedAnimation:Stop() -- Stop the animation
end
end)
Thank you!