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
9 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

local player = script.Parent.Parent
local mouse = player:GetMouse()
local sword = player.PlayerGui.UI.Armor.Sword.ItemClass
local A = Instance.new("Animation")
A.AnimationId = "http://www.roblox.com/Asset?ID=182377268"
local B = Instance.new("Animation")
B.AnimationId = "http://www.roblox.com/Asset?ID=182377825"

mouse.KeyDown:connect(function(key)
    if sword.Value == "Sword" then
        local animTrack = player.Character.Humanoid:LoadAnimation(A)
        animTrack:Play()
    elseif sword.Value == "Axe" then
        local animTrack = player.Character.Humanoid:LoadAnimation(B)
        animTrack:Play()
    end
end)
0
In my opinion, you did the link wrong, try changing it to 'http://www.roblox.com/asset/?id=182377825'. TheeDeathCaster 2368 — 9y

1 answer

Log in to vote
1
Answered by 9 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

repeat wait()
player = game.Players.LocalPlayer;
until (player) and (player.Character)

local mouse = player:GetMouse();
local sword = player.PlayerGui.UI.Armor.Sword.ItemClass;
local A, B = Instance.new("Animation"), Instance.new("Animation");
A.AnimationId = "http://www.roblox.com/asset/?id=182377268";
B.AnimationId = "http://www.roblox.com/asset/?id=182377825";

mouse.Button1Down:connect(function()
    if sword.Value == "Sword" then
        local animTrack = player.Character.Humanoid:LoadAnimation(A);
        animTrack:Play();
    elseif sword.Value == "Axe" then
        local animTrack = player.Character.Humanoid:LoadAnimation(B);
        animTrack:Play();
    end
end)

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 — 9y
0
This method still works though. I've used it on the other objects in rare occasions. YasuYoshida 171 — 9y
Ad

Answer this question