“Attempt To index nil with GetMouse()” Error for Plugin

Hi, I’m making a plugin and it’s doing this for whatever reason. I’m defining it in a module script, does it have something to do with that?

By the way, I’m using plugin:GetMouse(), not player:GetMouse()

LocalPlayer is nil when using it in plugin context, use

local mouse = plugin:GetMouse() instead

That’s what I used. Forgot to mention it sorry

yeah it might be the module script then, does it work in a local script?

It’s all server sided modules, yes

Well that’s the problem, you can’t get the mouse from the server

This error actually isn’t with the GetMouse function: the error means that you’re trying to find a property named GetMouse of nil, which isn’t possible. The error means that the code is basically running:

nil:GetMouse()

TL;DR: the error means that plugin is nil.

Since plugin is a global, this probably means you’re running the code in a spot where plugin isn’t available (e.g. you’re not running the script in plugin test mode, I think you right click the script and press something) or (very unlikely) your code accidentally sets plugin to nil.

Edit:

Follow the instructions in the docs linked below to run the plugin properly:

(A copy-paste of the docs)

Saving a Plugin Script

Plugins start from scripts. To create a plugin, create a Script and save it as a plugin using the Explorer. For example, to create the EmptyScriptAdder Plugin:

  1. Insert a new Script inside ServerStorage and rename it to EmptyScriptAdder.

  1. Copy and paste the EmptyScriptAdder Plugin code into the new script.

  2. In the Explorer window, right-click the script and select Save as Local Plugin.

  3. In the popup window, click Save to insert the plugin script into your local Plugins folder of the Studio installation.

  4. The plugin should appear in PluginDebugService and start running.

Make sure to delete the original script in ServerStorage and work from the plugin inside PluginDebugService, otherwise you may end up applying changes to the wrong script.

Reloading and Saving Changes

With your Plugin inside PluginDebugService, you can easily update the plugin by right-clicking it and then selecting Save and Reload Plugin from the context menu. If you simply want to reload the plugin, for example to step through a section of code using a breakpoint without saving the plugin, you can alternatively select Reload Plugin.

1 Like

Note: If your trying to access the plugin global variable from a module script it will not work:
image

Why? No idea.

You’ll also need to make sure plugin:Activated(true) was ran, otherwise mouse events will not fire.