Dance stops upon pressing a key

Greetings developers!
I’ve been trying to make so a dancing animation stops when a player presses the moving keys, WASD and SpaceBar, using the UserInputService. But since i am not a very good scripter i tested and it did not work. The dance animation is still playing.
Basically i want to make so the animation stops when a player starts moving . Can somebody help me?

Here is the script :

local userInput = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
repeat wait()
	character = player.Character
until character
local hum = character:WaitForChild("Humanoid")
local emote = hum:LoadAnimation(script.Parent.Emote)
local playing = false

script.Parent.MouseButton1Click:Connect(function(Dance)
	if playing == false then
		emote:Play()
		playing = true
	elseif playing == true then
		emote:Stop()
		playing = false
	end
end)

userInput.InputBegan:Connect(function(input)
	if input.KeyCode == { Enum.KeyCode.W;
						  Enum.KeyCode.A;
						  Enum.KeyCode.S;
						  Enum.KeyCode.D;
						  Enum.KeyCode.Space }
	then						
	emote:Stop()		
	end
end)

The script is located inside the dancing button beside the animation itself.
image
Thank you for helping <3

1 Like

It isn’t working because you’re comparing two incompatible types: an EnumItem and a table of EnumItems. If you want to compare against all of them you should loop over a table and compare against each EnumItem individually. Like so:

local invalidInputs = {
	Enum.KeyCode.W,
	Enum.KeyCode.A,
	Enum.KeyCode.S,
	Enum.KeyCode.D,
	Enum.KeyCode.Space
}

UIS.InputBegan:Connect(function(inputObj, gameProcessed)
	if not gameProcessed then
		for _,keyCode in next, invalidInputs do
			if inputObj.KeyCode == keyCode then
				emote:Stop()
			end
		end
	end
end)
2 Likes

Another way to do it would be using ContextActionService.

local ContextActionService = game:GetService("ContextActionService")
local invalidInputs = {
	Enum.KeyCode.W,
	Enum.KeyCode.A,
	Enum.KeyCode.S,
	Enum.KeyCode.D,
	Enum.KeyCode.Space
}

local function StopDance()
      animation:Stop()
end

ContextActionService:BindAction("StopDancing", StopDance, false, unpack(invalidInputs))
1 Like

P.S: Instead of iterating through the invalidInputs array, you can simply use the quite new table.find function to evaluate if the KeyCode is inside the array:

if (not gameProcessed and table.find(invalidInputs, inputObj.KeyCode)) then
    --// Code
end

Also, since this is only integrated to use the KeyCodes it’ll only work for when the Player moves on a device using a Keyboard. Instead of using KeyCodes, you can use ContextActionService (which @starmaq mentioned) but with PlayerAction EnumItems (such as CharacterJump, CharacterForward etc).

1 Like

@ReturnedTrue , @starmaq
After working on different things i’ve decided to finally fix the dancing animation so they can stop even when a mobile player moves.

As starmaq mentioned, i used ContextActionService and indeed it works on PC/Laptop with CharacterJump and others. However, i tested it on studio with the mobile thingy (i don’t know how it’s called) and it doesnt seem to work.

Here is the recent script :

local ContextActionService = game:GetService("ContextActionService") 
		--[Defining stuff for animation]
local player = game.Players.LocalPlayer
local character = player.Character
repeat wait()
	character = player.Character
until character
local hum = character:WaitForChild("Humanoid")
local emote = hum:LoadAnimation(script.Parent.Emote)
local playing = false
		--[Dance upon click]
script.Parent.MouseButton1Click:Connect(function(Dance)
	if playing == false then
		emote:Play()
		playing = true
	elseif playing == true then
		emote:Stop()
		playing = false
	end
end)
		--[Stops dance upon moving]
local invalidInputs = {
	Enum.PlayerActions.CharacterBackward,
	Enum.PlayerActions.CharacterForward,
	Enum.PlayerActions.CharacterJump,
	Enum.PlayerActions.CharacterLeft,
	Enum.PlayerActions.CharacterRight
}

local function StopDance()
      emote:Stop()
		playing = false
end

ContextActionService:BindAction("StopDancing", StopDance, false, unpack(invalidInputs))
1 Like