Is there a better alternative than mouse.Target?

I’ve been spending some time creating a plugin, and I’ve been trying to obtain the status of the Mouse. When I’m obtain the PluginMouse, mouse.Target just seems so inaccurate when I’m hovering my mouse on different areas.

For example, here’s a snippet of something I tested:

local mouse = plugin:GetMouse()

while true do
   wait(1)
   print(mouse.Target)
end

The output gives me this despite waving my mouse in different areas multiple times:


(Ignore the highlighted text in the picture.)

Heck, if I move the entire camera viewpoint (while my mouse is on a specific area,) it’ll sometimes change.

It’s so inaccurate at updating its status – and even it if happens, there’s like a 1/999 chance that it’ll work, it ever does it so rarely.

Is there a better alternative to this I can use? I’ve been staring at the documentation page for plugin:GetMouse for a few hours now, and I’ve got nothing. Am I going about it wrong?

4 Likes

I honestly don’t think there is a better alternative to the mouse.Target thing. But if you’re getting a nil value, MAYBE (I’m not sure) it’s because the vector position of the mouse isn’t matching the magnitude of the baseplate. But again, I’m not sure.

After researching a bit more, I found this solution to be more suitable:

mouse.Move:Connect(function()
	local target = mouse.Target

	for _,blocks in pairs(workspace:GetDescendants()) do
		if target == blocks and blocks:IsA("BasePart") then
			print(target)
		end
	end
end)

If anybody feels they have a solution more efficient than this, feel free to post.

Edit: was previously marked as a solution, but somebody pointed out that this method is very impractical and can be damaging, so I unmarked it.

6 Likes

I’m highly afraid this will cause CPU to explode. Calling complex functions when the mouse.Move fires is already very laggy, especially because it fires for each pixel on the screen. GetDescendants() on the whole workspace will work fine when you are on a baseplate, but in a bigger game calling it each time mouse’ position changes just by a pixel will complete the disaster. And also I’m not really sure how that code solves the problem, it just goes through all instances in workspace, and if mouse points on a part, something happens. This is the same as just using Target. Correct me if I’m wrong.

About the problem, I don’t really understand what you’re struggling with. Nil is always being returned when mouse points at skybox. I did some testing, and for me it always worked correctly. I’m not sure if this is relevant, but I plugin:Activate(true) before getting the mouse. Maybe that’s the fix, I don’t know.

2 Likes

You’ll be thankful to know that this code ended up on the cutting room floor.

I’ve realized at how impractical and inefficient of a way this was which honestly led to me scrapping the entire thing altogether.

Glad to hear I didn’t make a mistake.

However, even given the code, I still had problems with it as a whole. I figured out that it has to do something with the Camera and how the Plugin interacts with it:

-- snippet from my plugin code
    local cameraChildren = camera:GetChildren()

    mouse.Button1Down:Connect(function()
    	local hitPos = mouse.Hit
    	
    	--If selection box was accidentally deleted
    	if cameraChildren == nil then
    		selectionBox() --function that produces another SelectionBox within the camera
    	end
	
    	if hitPos == nil then
    		camera.SelectionBox.Adornee = nil
    		return
    	end

    	camera.SelectionBox.Adornee = mouse.Target
    end)

The problem is that the camera is having a hard time how to handle this. It’s like the mouse cursor and the camera aren’t getting along. When I don’t mess with the camera, this code is barely working, but when I mess with it while using the plugin, this code works great no problem.

I’m trying to create a plugin where when you click a Part/Model, the objects within it will be completely renamed and given their own unique names. (Sorta like Blender and how it gives its instances unique names when it a copy, hence why I’m exploring with Mouse.Target as a test.)

As of right now, I’m just testing the waters of it since it’s my first time actually creating a plugin for myself, and how vague it seems to be on the DevHub, barely any tutorial videos on it.

I’m just trying to figure out how to have the mouse cursor select a part and have a SelectionBox around that clicked Part.

I’m creating a game game concept that’s going to be based on selecting specific parts within a complex model, and it really doesn’t help when all the objects within that model are named Part.

In all honesty, I could just go through the entire model of 160 parts and start naming them one by one, but by that point it just seems tedious and overwhelming.

Sorry for the long response.

Edit: I might take the solution response of mine off, depending on where this conversation leans toward. We’ll see.

What do you mean? Is it the mouse going crazy and picking random stuff as target?

What kind of mess are you doing with the Camera? Target worked perfectly with my default camera, so that “mess” probably has something to do with it.
Also, why do you get children of the camera?

Sorry if I sound rude, I just want to know more

What do you mean? Is it the mouse going crazy and picking random stuff as target?

What kind of mess are you doing with the Camera? Target worked perfectly with my default camera, so that “mess” probably has something to do with it.

If you’re implying the default camera as the Client view, sure that’s normal. But for in-studio when the game isn’t running, Target does not interact well with Camera for somewhat reason. Whenever I go in the properties of Camera and change the CameraSubject, the problem will automatically fix itself.

Before playing around with the Camera properties, when ever I would try to click a Part, the mouse would randomly select something else that is not what I clicked.

That’s me excessively clicking everywhere with my mouse and the script often won’t respond.

Also, why do you get children of the camera?

I gather the children of the camera to destroy any SelectionBoxes that is in the Camera, if any.

By this point, I think it’ll be a bit more beneficial for me to post the entire code of what I’m doing, maybe that’ll clear the air:

local toolbar = plugin:CreateToolbar("Custom Script Tools")
local OBJ_Button = toolbar:CreateButton("Title","Desc","image id")
local isOn = false

local gui = script:WaitForChild("OBJPlugin_Gui")
gui.Parent = game:GetService("CoreGui")
local selectionName_Label = gui.BG.SelectionName_Label
local location_Label = gui.BG.Location_Label

local initiate_Button = gui.BG.Initiate_Button
local mouse = plugin:GetMouse()

local camera = game.Workspace.Camera

function selectionBox()
	local selectionBox = Instance.new("SelectionBox")
	selectionBox.LineThickness = 0.1
	selectionBox.Color3 = Color3.fromRGB(118, 104, 255)
	selectionBox.Adornee = nil
	selectionBox.Parent = camera
end

OBJ_Button.Click:Connect(function()
	isOn = not isOn
	OBJ_Button:SetActive(isOn)
	
	if isOn == true then
		print("Opened")
		
		local camSubject = camera.CameraSubject
		selectionBox()
		
		plugin:Activate(true)
	else
		--If any SelectionBoxes are in the Camera
		camera:ClearAllChildren()
		
		plugin:Activate(false)
		print("Closed")
	end
	
	gui.Enabled = isOn
end)

initiate_Button.MouseButton1Click:Connect(function()	
	print("Invalid Selection")
end)

local cameraChildren = camera:GetChildren()

mouse.Button1Down:Connect(function()
	local hitPos = mouse.Hit
	
	--If selection box was accidentally deleted
	if cameraChildren == nil then
		selectionBox()
	end
	
	if hitPos == nil then
		camera.SelectionBox.Adornee = nil
		return
	end

	camera.SelectionBox.Adornee = mouse.Target
end)

I’m not sure if it’s a bug that I rarely have or if it’s a problem that’s happening within my script. Maybe it has something to do with my script?

I just realized that it has nothing to do with the camera at all. It has something to do on Roblox’s end, where the cursor will act up and not work properly. But after clicking out of studio and going back in, everything will work fine.

I’m not sure if this is known when making a plugin, but do let me know if necessary, and how I can combat that problem if I’m able to.

1 Like