Player = game.Players.LocalPlayer Mouse = Player:GetMouse() Mouse.KeyDown:connect(function(key) if (key == "q") then currentAnimTrack = Player.Character.Humanoid:LoadAnimation(319782924) currentAnimTrack:Play(319782924) end end)
i wanted that if a player presses q that it would play a animation but it doesn't work some1 help me plz thank you
Consider changing currentAnimTrack to local
This looks like my code I previously posted here: https://scriptinghelpers.org/questions/24331/how-do-u-create-moves
I ripped this from the ROBLOX animation script so it might need local in-front of "currentAnimTrack."
Player = game.Players.LocalPlayer Mouse = Player:GetMouse() Mouse.KeyDown:connect(function(key) if (key == "q") then local currentAnimTrack = Player.Character.Humanoid:LoadAnimation(319782924) currentAnimTrack:Play(319782924) end end)
mosski123 is wrong, local variables are meant to gone in the middle of the script. You put variables without local use on top.
In Lines 6 and 7, you try passing off a number value (assuming its the animation ID) within the parameters of the function(s). Problem with that is the script isn't going to know what to do with those values, they are just there. There are a number of workarounds for this problem, but here's what I would do in this scenario:
Place the LocalScript
inside of StarterPack
, and insert an Animation
object directly inside of that script. It should look something like this now:
Click on the Animation
object and inside the AnimationId
property, let's enter in the asset URL in order for the script to be able to locate it: rbxassetid://319782924
View the GIF here if you have any troubles.
Sweet, now that we got the animation set up, let's head into the actual scripting. I find it better to use UserInputService
instead of KeyDown
events (especially across multiple platforms), but it works either way in this case.
local deb = false -- setting up our debounce local key_input = game:GetService("UserInputService") local player = game.Players.LocalPlayer local mouse = player:GetMouse() repeat wait() until player.Character local char = player.Character -- set a variable to our character local anim = script:WaitForChild("Animation") --[[ What we've done so far is set up our variables we are going to need. Now we are going to code the actual function, in which the *Q* key is pressed and the animation plays. ]] key_input.InputBegan:connect(function(key) -- function fires when a key is pressed if (key.KeyCode == Enum.KeyCode.Q) and not deb then deb = true local animtrack = char.Humanoid:LoadAnimation(anim) -- load the animation animtrack:Play() -- play the animation print("Playing animation...") -- for debugging purposes end wait(1) deb = false -- wait 1 second before the animation can be played again end)
We should now have a working animation script that runs whenever the Q key is pressed! Here is a preview of how the script should work (NOTE: I'm using one of my own animations for this test):
Questions / comments? Feel free to message me and I'll answer them as soon as possible. If you are satisfied with the answer I provided, please upvote me. Thanks!