Help with Tool & UIS

Hello, I am trying to make a tool which clones a SelectionBox when the mouse is on a part. I am really confused on how to convert Vector2 to Vector3 or to use UserInputService. Here is my code.

local player = game.Players.LocalPlayer

local mousePos = player:GetMouse()

script.Parent.Equipped:Connect(function()
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("Part") and v.Name == "Dirt" then
			if mousePos == v.Position then
		       local highlight = game.ReplicatedStorage.SelectionBox
		       local higlightclone = highlight:Clone()
		       higlightclone.Parent = game.Workspace
				higlightclone.Adornee = v
			end
		end
	end
end)

script.Parent.Unequipped:Connect(function()
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("SelectionBox") and v.Name == "SelectionBox" then
		   v:Destroy()
		end
	end
end)
1 Like

You can use mouse.Target instead of checking position.

Ok. I’ll try it! Thanks for the feedback!

Would I have to use UIS or get the mouse from the player?

Nope. You don’t need to do that.

You can get player’s mouse from tool too.

like this:

local tool = (...) 
 
tool.Equipped:Connect(function(mouse)
    local target = mouse.Target
	if target then
        -- our code here
	end
end)

if you want your SelectionBox gone after Unequipped tool you can store your Selection in table

local Highlights = {}

    local tool = (...) 
     
    tool.Equipped:Connect(function(mouse)
        local target = mouse.Target
    	if target then
            local highlight = game.ReplicatedStorage.SelectionBox:Clone()
		    highlight.Parent = target
            highlight.Adornee = target

           table.insert(Highlights,highlight)
    	end
    end)

    tool.Unequipped:Connect(function(mouse)
        for i,v in pairs(Highlights) do
            v:Destroy()
        end
        table.clear(Highlights)
    end)

Let me know if my codes aren’t working, I didn’t test in studio yet.