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
plr.CharacterAdded:Wait()
local character = plr.Character
local humanoid = plr.Character:WaitForChild("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
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
animation: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!
Questions to ask:
Any errors in the output? Have you confirmed if the code runs after the if statement checking if the IsAttacking value is false? Maybe the IsAttacking value could potentially be true? Does the RemoteEvent even fire? Have you made sure that you pasted the correct animation id for HitAnimation?
I believe the issue is likely occurring from the if statement checking if the IsAttacking value is false. Make sure the IsAttacking value is set to false, and add a print statement under that if statement to see if it runs.
local character = plr.Character
local humanoid = plr.Character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = animator:LoadAnimation(script:WaitForChild("HitAnimation"))
use Humanoid.Animator:LoadAnimation() instead of Humanoid:LoadAnimation() since it’s deprecated
local plr = game.Players.LocalPlayer
local uis = game:GetService(“UserInputService”)
local hit = game.ReplicatedStorage.Hit
plr.CharacterAdded:Wait()
local character = plr.Character
local humanoid = plr.Character:WaitForChild(“Humanoid”)
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 animation = humanoid.Animator:LoadAnimation(script:WaitForChild("HitAnimation"))
animation: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)
local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local hit = game.ReplicatedStorage.Hit
plr.CharacterAdded:Wait()
local character = plr.Character
local humanoid = plr.Character:WaitForChild("Humanoid")
local animation = humanoid:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnimation"))
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
animation: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)
A script that controls the children of the character:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local attackbool = Instance.new("BoolValue", char)
attackbool.Name = "IsAttacking"
local cooldown = Instance.new("BoolValue", char)
cooldown.Name = "OnCooldown"
local animator = Instance.new("Animator", char:WaitForChild("Humanoid"))
animator.Name = "Animator"
end)
end)
remove WaitForChild(), and write script.HitAnimation instead of script:WaitForChild("HitAnimation"), if not you could check for that lines are can work,