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

Putting mouse from plugin in an object value isn't working?

To protect the source of the plugin I have the plugin use a loader type method, the module doesn't return a table of functions it runs on require(), to get the mouse object in the module I make the plugin loader put the mouse in a object value then put the object value in the coregui. The module then finds the object value and gets the mouse. The mouse works sort of I can get the mouse.Hit.p position but the events such as Button1Down and Button1Up do not work, No error or output is shown.

I've been using this to test it

spawn(function() -- Heavily commented because its not working
	print("1")
	print(Mouse) -- Prints instance
	Mouse.Button1Down:connect(function()
		PaintLoop = true
		print("M1Down")
	end)
	Mouse.Button1Up:connect(function()
		PaintLoop = false
		print("M1Up")
	end)
end)

1 does print, print(Mouse) prints "Instance" Both down and up functions never get called nor print anything.

1 comment

0

Have you defined Mouse?

1 answer

✓ Accepted answer

The plugin mouse is shared with studio so either studio has the mouse or the plugin not both. If the select tool or other tools are active then studio has the mouse meaning that the plugin will not fire any of the mouse events.

Note, you should be using the "plugin" variable and not the PluginManager. The tutorial shows how it should be used, click here.

To solve this problem simply give the mouse control back to the plugin:-

plugin:Activate(true)

api:- Activate

Lastly you do not need to an object value as you can pass the "plugin" object e.g.

Server script (main script)

local mouseActive = require(location  to module)(plugin) -- Passes the plugin object to the module

The mouse module, this is not tested. This is only an example

return function(plug)
	local mouse = plug:GetMouse()
	
	mouse.Button1Down:connect(function()
		print('Clicked')
	end)
	
	-- our event to give the mouse back to the plugin
	return function()
		plug:Activate(true) -- activates the mouse
	end
end

We can then use the function passed back:-

mouseActive() -- activates the mouse

This is all just an example, it may be useful to use the UserInputService to look for a key press to run the event e.g. esc to give the plugin the mouse.

I hope this helps, please comment if you do not understand how / why this code works.

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