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

How does the Mouse Button1Down event work?

I just created the following script:

-- Import animation
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=146374944"

-- Others
MyMouse = mouse
MyMouse.Button1Down:connect(playAnim)

-- Local variables
local animTrack = nil
local canPlay = true
 
function playAnim(Source)
	if canPlay then
		local player = script.Parent.Parent
		canPlay = false
		animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
		wait(2)
		canPlay = true
		end	
		animTrack:Play() -- Start the animation
	end

But it gives me the following Error:

18:32:57.308 - Workspace.Player1.ID [C5].Script:20: attempt to index global 'mouse' (a nil value)

18:32:57.310 - Script 'Workspace.Player1.ID [C5].Script', Line 20

18:32:57.310 - stack end

How can I fix it. Please note the following: -This is a tool -This is supposed to run an Animation in the Humanoid. It works perfectly without MouseClicks

1 comment

0

local mouse = player:GetMouse()

1 answer

You're not using any events here! mouse isn't just some magical global variable, you need to get it from somewhere. The mouse object is given as the first argument in the Equipped event of tools.


script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		print("Blockhaak down!")
	end)
end)

3 comments

0

Thanks, you really helped me! (I would Up-vote, but I cant xD)

0

Thanks, you should be able to accept the answer, though.

1

Excellent answer!

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