Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My Animation Will Not Play When I Press "X" , What is Wrong Here?

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()


local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=5191066147"

mouse.KeyDown:connect(function(key)
  if key == "x" then
   local playAnim = humanoid:LoadAnimation(anim)
   playAnim:Play()

    end
end)

2 comments

0

The code on line 2 is faulty

0

The players character could by chance not exist, or the Humanoid might not exist, resulting in a Runtime error.

1 answer

✓ Accepted answer

Two things;

Your animation doesn't have a parent, essentially rendering it nil (you should also use "rbxassetid://YourID" for the AnimationId.

anim.Parent = script

Use UserInputService.InputBegan instead of mouse.KeyDown

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input,event)
	if input.KeyCode == Enum.KeyCode.X and not event then
		-- do stuff
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.