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

How do I play a animation when I press a a certain key?

I tried to make a animation play by pressing Q. Nothing happened. Can you guys help me out?

local UserInputService = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer

local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=492283384"

local animTrack = nil
local canPlay = true

local function onInputBegan(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        if keyPressed == Enum.KeyCode.Q then
	local player = game.Players.LocalPlayer.Character
		canPlay = false
		animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
		animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
			if keyframeName == "ElectrocuteEnd" then
				canPlay = true
end

end
          
    end
end
 
local function onInputEnded(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        if keyPressed == Enum.KeyCode.Q then
     local player = game.Players.LocalPlayer.Character
		canPlay = false
		animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
		animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
			if keyframeName == "ElectrocuteEnd" then
				canPlay = false
		end
    end
end
 
UserInputService.InputBegan:connect(onInputBegan)
UserInputService.InputEnded:connect(onInputEnded)

2 comments

0

That's a free model script though...

0

No. It's not.

1 answer

✓ Accepted answer

You can use this instead its not mine so i wont use it for my game ;p


	 Animation = Instance.new("Animation")
	Animation.Parent = game.Workspace
	Animation.AnimationId = "http://www.roblox.com/asset/?id=ID HERE" --Where It Says ID HERE, Place Your Animation Id
	local Player = game.Players.LocalPlayer
	local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function (key)
	for i, v in pairs(game.Players:GetChildren()) do
	if key == "f" then -- THIS IS UR KEY U WANA CHANGE IT CHANGE F TO SOMETHING ELSE
		local AnimationTrack = Player.Character.Humanoid:LoadAnimation(Animation)
			AnimationTrack:Play()
	end
	end
	end)

That is all ;)

Dont forget to accept my answer

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