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

Mouse keydown animation problem?

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)

1 comment

0

In my opinion, you did the link wrong, try changing it to 'http://www.roblox.com/asset/?id=182377825'.

1 answer

✓ Accepted answer

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!

2 comments

0

You do not have to wait for LocalPlayer. You should wait for PlayerGui and its descendants.

0

This method still works though. I've used it on the other objects in rare occasions.

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