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

Mouse keydown animation problem?

Asked by
Paldi 109
10 years ago

I have a script to make my character do a animation when i click with the mouse button but it does the animation only when im walking or jumping, and not by clicking with the mouse.

ps: this is a local script in the StarterPack

01local player = script.Parent.Parent
02local mouse = player:GetMouse()
03local sword = player.PlayerGui.UI.Armor.Sword.ItemClass
04local A = Instance.new("Animation")
06local B = Instance.new("Animation")
08 
09mouse.KeyDown:connect(function(key)
10    if sword.Value == "Sword" then
11        local animTrack = player.Character.Humanoid:LoadAnimation(A)
12        animTrack:Play()
13    elseif sword.Value == "Axe" then
14        local animTrack = player.Character.Humanoid:LoadAnimation(B)
15        animTrack:Play()
16    end
17end)
0
In my opinion, you did the link wrong, try changing it to 'http://www.roblox.com/asset/?id=182377825'. TheeDeathCaster 2368 — 10y

1 answer

Log in to vote
1
Answered by 10 years ago

You should use Button1Down not KeyDown. KeyDown is used for keyboard keys like : WASD. Button1Down is when you left click. Also you should have the script wait before the player loads, you might have problems in online mode without it.

http://wiki.roblox.com/index.php?title=API:Class/Mouse/Button1Down

http://wiki.roblox.com/index.php?title=API:Class/Mouse/KeyDown

01repeat wait()
02player = game.Players.LocalPlayer;
03until (player) and (player.Character)
04 
05local mouse = player:GetMouse();
06local sword = player.PlayerGui.UI.Armor.Sword.ItemClass;
07local A, B = Instance.new("Animation"), Instance.new("Animation");
10 
11mouse.Button1Down:connect(function()
12    if sword.Value == "Sword" then
13        local animTrack = player.Character.Humanoid:LoadAnimation(A);
14        animTrack:Play();
15    elseif sword.Value == "Axe" then
16        local animTrack = player.Character.Humanoid:LoadAnimation(B);
17        animTrack:Play();
18    end
19end)

Don't forget to mark this as solved and as the correct answer!

0
You do not have to wait for `LocalPlayer`. You *should* wait for PlayerGui and its descendants. BlueTaslem 18071 — 10y
0
This method still works though. I've used it on the other objects in rare occasions. YasuYoshida 171 — 10y
Ad

Answer this question