Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this animation script when i press a key not working?

Asked by 6 years ago

I press the l key and nothing happens, why is this? The script is in the starter pack and it has an animation with the correct code in it. I can't figure out why this doesn't work, and i'm not very good with scripts.

https://gyazo.com/8e4569e28405b485b7f2627a19acb3c3

4 answers

Log in to vote
0
Answered by 6 years ago

It does not work due to the fact that KeyDown is deprecated. Instead, use ContextActionService. And as previous answers said, you defined the player as LocalPlayer but used player.

function playAnimation()
    local plr = game:GetService('Players').LocalPlayer
    local anim = script.Animation
    local track = plr.Character.Humanoid:LoadAnimation(anim)
    track:Play()
end

game:GetService('ContextActionService'):BindAction(
    "PlayAnim",
    playAnimation,
    false,
    Enum.KeyCode.One
)

This will work since it has no deprecated code.

0
Even though KeyDown is deprecated, it still works... JackOfAllBlox 32 — 6y
0
Not for me. I tried it just for fun and it says 'KeyDown' is not a valid member of Mouse User#19524 175 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

It doesn't work cause you defined your player as LocalPlayer and later used player

What you can do to fix it is just to change every place where you have player in your script to LocalPlayer

0
Still doesn't work. Undead_Ikimono 5 — 6y
Log in to vote
0
Answered by 6 years ago

Basically what they are both saying is that you forgot to put a space between local and player, lol.

0
Still nothing .-. Undead_Ikimono 5 — 6y
Log in to vote
-1
Answered by 6 years ago

It's because you declared your variable as LocalPlayer but are trying to reference it by typing player. In fact, Lua automatically will show a blue line under variables it considers undefined (The blue lines under player in your screenshot).

The fixed code would change LocalPlayer to player or player to LocalPlayer:

wait(2)

player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
Anim = script.Animation

Mouse.Keydown:connect(function(key)
    if key == '1' then
        a = player.Character.Humanoid:LoadAnimation(Anim)
        a:Play()
    end
end)

Also, KeyDown is deprecated, meaning there is a better newer different version of it you should use instead. If you are comfortable with keydown thats fine, but it's better to use UserInputService or ContextActionService. Check out this page on the wiki:

http://wiki.roblox.com/index.php?title=Keyboard_input

Answer this question