Buggy Dance Script

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!

1 Like
local UserInputService = game:GetService("UserInputService");

local Character = script.Parent;
local Humanoid = Character:WaitForChild("Humanoid");
local Animator = Humanoid:FindFirstChildWhichIsA("Animator");
local Animation = script:WaitForChild("DanceAnimation");

local LoadedAnimation: AnimationTrack;

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean): ()
	if (gameProcessedEvent) then
		return;
	end;
	if (input.KeyCode == Enum.KeyCode.J) then
		if (LoadedAnimation) then
			if (LoadedAnimation.IsPlaying) then
				LoadedAnimation:Stop();
				return;
			end;
			LoadedAnimation:Play();
		else
			LoadedAnimation = Animator:LoadAnimation(Animation);
			LoadedAnimation:Play();
		end;
	end;
end);

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function(): ()
	if (Humanoid.MoveDirection.Magnitude > 0) then
		if (LoadedAnimation and LoadedAnimation.IsPlaying) then
			LoadedAnimation:Stop();
		end;
	end;
end);
1 Like

Try to decrease the debounce instead wait(10) set it to maybe somewhere wait(0.25) to wait(1)

3 Likes

I honestly have no idea how I missed this, thank you for this simple and easy solution!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.