My script for playing a animation is broken

I was making a script for playing a animation, the script doesn’t work, can someone tell me how to fix the script?

Here are my problems:

  1. I want to fix my broken script

  2. Here is the issue:

Here is my script:

local player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local debounce = false

UserInputService.InputBegan:Connect(function(input, isProcessed)
	if isProcessed then
		return
	end

	if input.KeyCode == Enum.KeyCode.E then
		if debounce then
			return
		end

		debounce = true
		game.ReplicatedStorage.CombatHit:FireServer()

		local animation = Instance.new("Animation")
		animation.AnimationId = "17017381266" 
		local animTrack = player.Character.Humanoid:LoadAnimation(animation)
		animTrack:Play()

		print("E key is pressed")

		wait(3) 

		debounce = false
	end
end)

I haven’t completed the script yet so I can test it and later on I’m going to complete this script

1 Like

I think you need to put in a Roblox asset id prefix.

"rbxassetid://ASSET_ID_HERE"
"http://www.roblox.com/asset/?id=ASSET_ID_HERE"

I had the same issue, but it was fixed when I put one of these in.

  1. Just as @12345koip said, your missing the base URL

  2. Your creating a whole new animation each time you press a button. Instead this is what you should do:

local uis = game:Get service("UserInputService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() -- this wait part should yield the code until the character exists.

local hum = char:WaitForChild("Humanoid")

local anim = Instance.new("Animation")
anim.AnimatonId = "rbxassetid://".."ANIMATION.ID.HERE"

local loaded_anim = hum:WaitForChild("Animator"):LoadAnimation(anim)
anim:Destroy() -- destroy the animation object because it is useless now


uis.InputBegan:Connect(function(key, proc)
anim:Play()
end)

This is just an example.

I wrote this on my phone. So, some of it may be off.