Animation not playing

Hello. I am trying to make it so an animation plays when you press the “Q” key, but the animation won’t play. I have not been able to find a working solution to solve this.

Script:

game.ReplicatedStorage.Hit.OnServerEvent:Connect(function(plr, class)
	if class == 1 then
		plr.Character.IsAttacking.Value = true
		game.ReplicatedStorage.Hit.OnServerEvent:Wait()
		plr.Character.IsAttacking.Value = false
	end
end)

LocalScript:

local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local hit = game.ReplicatedStorage.Hit

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character.Humanoid.WalkSpeed += 8
	end
	if key.KeyCode == Enum.KeyCode.RightShift then
		plr.Character.Humanoid.WalkSpeed += 8
	end
	if key.KeyCode == Enum.KeyCode.Q then
		print(tostring(plr.Character.IsAttacking.Value))
		if plr.Character.IsAttacking.Value == false then
			local anim = plr.Character:WaitForChild("HitAnimation")
			local track = plr.Character.Humanoid:LoadAnimation(anim)
			track:Play()
			hit:FireServer(1)
		end
		print("q")
	end
end)
uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character.Humanoid.WalkSpeed -= 8
	end
	if key.KeyCode == Enum.KeyCode.RightShift then
		plr.Character.Humanoid.WalkSpeed -= 8
	end
end)

Both scripts are parented to StarterCharacterScripts.
Thanks!

2 Likes

Try this

local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local hit = game.ReplicatedStorage.Hit
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = plr.Character.Humanoid
local animation = humanoid:LoadAnimation(script:WaitForChild("HitAnimation"))

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character.Humanoid.WalkSpeed += 8
    elseif key.KeyCode == Enum.KeyCode.RightShift then
        plr.Character.Humanoid.WalkSpeed += 8
	end
end)

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Q then
		if plr.Character.IsAttacking.Value == false then
			animation:Play()
			hit:FireServer(1)
		end
	end
end)

uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character.Humanoid.WalkSpeed -= 8
	elseif key.KeyCode == Enum.KeyCode.RightShift then
		plr.Character.Humanoid.WalkSpeed -= 8
	end
end)
2 Likes

Can you explain what exactly you changed with this script?

1 Like

Still didn’t work, are there any properties of animations that might be causing this or is it my script?

Parent the animation to the localscript like this
image
Rename the Animation to whatever you prefer to name it as

Alright. I will test that out, thanks!

That didn’t work, it might have to do with the properties of animations.

Change this line to this:
local animation = humanoid:LoadAnimation(script:WaitForChild("HitAnimation"))
If your animation is called something else, just change the name
Can I also see your hierarchy?

What hiearchy? hkhfsaafsdsfddas

In the StarterCharacterScripts

It’s just game.StarterPlayer.StarterCharacterScripts

No in the Explorer‍‍‍

4

This might be stupid but can you move the localscript to StarterPlayerScripts?

Alright, still won’t work. It might have to do with the animation.

Try this

local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local hit = game.ReplicatedStorage.Hit
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = plr.Character.Humanoid
local animation = humanoid:LoadAnimation(script:WaitForChild("HitAnimation"))

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character.Humanoid.WalkSpeed += 8
    elseif key.KeyCode == Enum.KeyCode.RightShift then
        plr.Character.Humanoid.WalkSpeed += 8
	end
end)

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Q then
		if plr.Character.IsAttacking.Value == false then
			animation:Play()
			hit:FireServer(1)
		end
	end
end)

uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		plr.Character.Humanoid.WalkSpeed -= 8
	elseif key.KeyCode == Enum.KeyCode.RightShift then
		plr.Character.Humanoid.WalkSpeed -= 8
	end
end)

Didn’t you already send this in earlier replies?

That is false, actually. LocalScripts are preferred because it has less latency than server, not because server does not work.

I revised it, put the localscript back in StarterCharacterScripts‍‍‍‍

Ah, alright. I was told it only works on client by other devs, thanks for clarifying!