Whats wrong with my script? (play animation while key pressed)

im trying to have my script play an animation and set a bool value to true while the key is pressed. i dont get any errors in the output but at the same time nothing is printing in the output

local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid") --gets the humanoid
local anim1 = hum:LoadAnimation(script:WaitForChild("BlockAnim"))

anim1:Play()
game:GetService("UserInputService").InputBegan:connect(function(input) -- Input Began checks if the player presses anything and fires whenever the player does so.
	if input.KeyCode == Enum.KeyCode.F then -- This detects for a "e" press.
		print("1")
		char.Blocking.Value = true
		anim1:Play()
	end
end)

game:GetService("UserInputService").InputEnded:connect(function(input) -- Fires when input ended
	print("2")
	char.Blocking.Value = false
	anim1:Stop()
end)
1 Like

dont mind the notes, i forgot to delete them

What are the parameters of your Remote Event? That may be the issue.

Also, put if input.KeyCode == Enum.KeyCode.F after the Input ended event

1 Like

im not using a remote event, its just all in that local script, i forgot to delete the notes

Uh why are you not getting the humanoid for the character variable?

1 Like

wdym? at line 2 i get the humanoid.

That is not what I meant, you have the character variable, but you don’t use it to get the humanoid in line 2…

1 Like

I edited the top message and updated the script to how it should look.

the script is updated yet it doesnt work…

never mind it is working thank you for the help, i had to change the humanoid on line 2

Print the character to see if it is even loaded.

1 Like

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local hum = char:FindFistChild(“Humanoid") --gets the humanoid
local anim1 = hum:LoadAnimation(script:WaitForChild(“BlockAnim”))

anim1:Play()
game:GetService(“UserInputService”).InputBegan:connect(function(input, IsTyping) – Input Began checks if the player presses anything and fires whenever the player does so.

if IsTyping then return end

if input.KeyCode == Enum.KeyCode.F then -- This detects for a "e" press.
	print("1")
	char.Blocking.Value = true
	anim1:Play()
end

end)

game:GetService(“UserInputService”).InputEnded:connect(function(input, IsTyping) – Fires when input ended
if IsTyping then return end
print(“2”)
char.Blocking.Value = false
anim1:Stop()
end)

1 Like