Emote Script Issue

I have a script here that plays an animation when you press J however if I press J again it doesn’t stop the emote. Also I was wondering if someone could make adjustments so the emote wont play or will stop playing if you are moving. Thank you!

Here’s the script:

local Humanoid = script.Parent:WaitForChild("Humanoid")

local Animation = script:WaitForChild("Dance")

local UIS = game:GetService("UserInputService")

local debounce = false

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.J then
		local LoadedAnimation = Humanoid:LoadAnimation(Animation)

		if debounce then return end
		debounce = true

		LoadedAnimation:Play()

		wait(10)
		debounce = false
	end
end)

Try this:

local Humanoid = script.Parent:WaitForChild("Humanoid")

local Animator = Humanoid:FindFirstChildOfClass("Animator") -- Humanoid:LoadAnimation is deprecated, use Animator:LoadAnimation instead.

local Animation = script:WaitForChild("Dance")

local UIS = game:GetService("UserInputService")

local debounce = false

local LoadedAnimation = Animator:LoadAnimation(Animation) -- Humanoid:LoadAnimation is deprecated, use Animator:LoadAnimation instead.
	
UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.J then

		if debounce then return end
		debounce = true

		if LoadedAnimation.IsPlaying then -- Check if the animation is currently playing
			LoadedAnimation:Stop() -- Stop the animation if it's already playing
		else
			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.IsPlaying then -- Check if the animation is currently playing
		LoadedAnimation:Stop() -- Stop the animation
	end
end)

Let me know if there are any issues or if you have any questions.

1 Like

You forgot to put an AnimationTrack:Stop() when you press J again. you only did LoadedAnimation:Play()

For the second part, I recommend looking at docs and checking if the state of the humanoid is walking then do AnimationTrack:Stop()
For example:

--In a local script
local humanoid = game:GetService("Players").LocalPlayer.Character.Humanoid
if humanoid then
    if humanoid:GetState() == Enum.HumanoidStateType.Running then
        LoadAnimation:Stop()
    end
end

Obviously there are some adjustments to this sample but this is a rough idea. I recommend checking the code samples of the docs.

it stops the animation if i try to move however it doesn’t stop it when i press J, also it doesn’t play again after I stop it once.

Try this:

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)
1 Like

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