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=" -- id of animation mouse.KeyDown:connect(function(key) if key == "l" then local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end)
Why your script isn't working is because KeyDown
is deprecated and has been removed by roblox, Instead use UserInputService
or ContextActionService
to replace KeyDown
. UserInputService
has the InputBegan event which fires when the player interacts with a device, such as pressing down a key, etc. Then you could use the "input" parameter to check the key that was pressed using the KeyCode Enum.
:connect()
is deprecated, so instead use :Connect()
. Use the CharacterAdded
event instead of a repeat loop, and use :WaitForChild()
to get the humanoid.
local player = game.Players.LocalPlayer player.CharacterAdded:Wait() local humanoid = player.Character:WaitForChild("Humanoid") local mouse = player:GetMouse() local anim = Instance.new("Animation") local UIS = game:GetService("UserInputService") anim.AnimationId = "http://www.roblox.com/asset/?id=" -- put the id here anim.Parent = player.Character UIS.InputBegan:Connect(function(input) if input.KeyCode = Enum.KeyCode.I then local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end)
Local scripts only affect things the user sees (like GUIs, camera or local parts). Server sided scripts can change things in the server, like the animation a player does or the CFrame of a part, for example. Local scripts can't change the server or affect other users, and server sided scripts are only used for... the server. You can use Remote functions/events
to let the client and server communicate with each other.
We're using remote events since we just want the animation to play. We will need 1 local script to record the input and 1 server sided script to play the animation.
--Remote Event is placed inside the server script in ServerScriptService script.RemoteEvent.OnServerEvent(function(player) --When the client fires the server... local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid then --If humanoid exists local anim = Instance.new("Animation") anim.AnimationId = "" --Fill this in local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end)
Local Script:
game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()
UserInputService
is much better than KeyDown (since UserInputService can get inputs from game controllers and touchscreen).
--Must be done in a local script game:GetService("UserInputService").InputBegan:Connect(function(key) if key.KeyCode== Enum.KeyCode.One then game:GetService("ServerScriptService").Script.RemoteEvent:FireServer() end end)
Use this site if you want to see the Enum index for keycodes.
Hope it helps!
Apparently animations can be triggered from a local script, so remote events aren't needed. Modified code:
game:GetService("UserInputService").InputBegan:Connect(function(key) local player = game:GetService("Players").LocalPlayer if key.KeyCode == Enum.KeyCode.One then local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid then --If humanoid exists local anim = Instance.new("Animation") anim.AnimationId = "" --Fill this in local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end end)
The reason why the animation may not be running is because Animations have different priorities dictating which animation be played over another. Here is a list of enums for AnimationPriority
. If you want an animation to be played, you should set its priority to something higher than the default animations (action is the highest priority). If an animation is played and another animation is played with the same priority, the first animation is stopped over the second one.
local anim = Instance.new("Animation") anim.AnimationId = "" --Fill this in game:GetService("UserInputService").InputBegan:Connect(function(key) local player = game:GetService("Players").LocalPlayer if key.UserInputType == Enum.UserInputType.Keyboard then --Make sure it is a keyboard input if key.KeyCode == Enum.KeyCode.One then local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid then --If humanoid exists local playAnim = humanoid:LoadAnimation(anim) playAnim.Priority = Enum.AnimationPriority.Action --Highest priority animation playAnim:Play() end end end end)
Change Enum...Action
to Enum...Core
if you want animations like walking, falling, jumping or swimming to override it.
a much better method would be doing:
local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid local humanoid = player.Character.Humanoid local mouse = player:GetMouse() Animation = script:WaitForChild("Animation") mouse.KeyDown:Connect(function(key) if key == "l" then --REMEMBER TO INSERT A ANIMATION INTO THE SCRIPT local Animationtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Animation) Animationtrack:Play() end end)