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

my script doesn't work why?

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

3 answers

Consider changing currentAnimTrack to local

7 comments

0

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() end end) it doesn't work like this

0

i stil dont get it but i will try

0

ty

0

really?!? thats so nice of you thanks!!!

0

i know how to make a jump animation but the thing is i wanted something else i wanted that if player presses Q it plays 319782924 its my selfcreated animation it is not a jumping animation or a running one its a action

0

just get in a certain stand

0

ty

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 here to view image.

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):

Preview GIF.

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!

3 comments

0

it isn't working i did exactly as you said i think the problem lies here you wrote in youre script -set a variable to our character. wich variable do i need to put in. could you write me the full code im very sorry to ask

0

Everything in the script should be running fine. Did you copy the script source and follow the set of instructions properly?

0

yes i did

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