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

How do i make a character do a animation when a key is pressed?

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("humanoid")
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://5255215899" 

function onKeyPressed (inputObject,gameProcessEvent)
	if inputObject.KeyCode == Enum.KeyCode.Q then
		humanoid:LoadAnimation (anim) :Play()
		print ("Animation is playing")
	end
end
uis.InputBegan:Conect (onKeyPressed)

so this is the code i have, but whenever i play the game in roblox studio, and press "Q" is says:

Infinite yield possible on 'Workspace.bencharles429:WaitForChild("humanoid")'

how do i fix this?

1 answer

Try This

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid") -- Humanoid With A CAPITAL H
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://5255215899" 

function onKeyPressed (inputObject,gameProcessEvent)
    if inputObject.KeyCode == Enum.KeyCode.Q then
        humanoid:LoadAnimation (anim) :Play()
        print ("Animation is playing")
    end
end
uis.InputBegan:Conect (onKeyPressed)

3 comments

0

i did that code and now it says: "Conect is not a valid member of RBXScriptSignal"

0

Just Change Conect To Connect

0

thx dude

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