Help with making a custom mouse ray?

currently I have a building system that relies on the mouse for moving buildings. however I need to make changed to the build system so now I need the mouse to both ignore the model its dragging AND ignore specific parts it hovers over. unfortunately mouse.TargetFilter only filters one instance so now I need to make a customized mouse to substitute the current mouse system. I tried this:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

function managecustommouse()
	local ray = Ray.new(game.Workspace.CurrentCamera.CFrame.p,mouse.Hit.p)
	local parts = workspace:FindPartOnRay(ray)
	print(parts)
end




mouse.Move:Connect(function()
	managecustommouse()
end)

this does not work quite as expected, it only prints nil unless my camera is directly under my character in which case it prints parts of my character. am I doing something wrong?

Credit to @realhigbead, he made this. Use CollectionService:AddTag(part)

local coll = game:GetService('CollectionService')
local function getMousePosition(mouse)
	local ray = Ray.new(mouse.Origin.p,mouse.UnitRay.Direction*2048)
	local ignore = coll:GetTagged('TargetIgnore')
	ignore[#ignore+1] = plr.Character
	local _,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
	return pos
end
3 Likes

^ This should work if you add the “TargetIgnore” CollectionService tag to the model you’re currently placing.

can you show me how? I have some of it hooked up and Im trying to print the object the ray finds as a test before tagging things to filter:

local coll = game:GetService('CollectionService')
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local function getMousePosition(mouse)
	local ray = Ray.new(mouse.Origin.p,mouse.UnitRay.Direction*2048)
	local ignore = coll:GetTagged('TargetIgnore')
	ignore[#ignore+1] = plr.Character
	local _,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
	return pos
end

function printpos()
	getMousePosition(mouse)
	print(mouse)
end


mouse.Move:Connect(function()
	printpos()
end)

Try printing Vector.X, Vector.Y, Vector.Z

that seems to be working, but just to let you know Z wasn’t valid with the mouse, only X and Y :stuck_out_tongue:

I’m confused, I thought that function returned a vector3, not a vector2.

that might be a problem not having the Z axis acually :confused:

when I use this: print(mouse.X,mouse.Y,mouse.Z)
an arror comes up: Z is not a valid member of playermouse

however if I remove mouse.Z, it prints x and y.

sorry if I prematurely marked this as a solution.

Ohh…

local coll = game:GetService('CollectionService')
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local function getMousePosition(mouse)
	local ray = Ray.new(mouse.Origin.p,mouse.UnitRay.Direction*2048)
	local ignore = coll:GetTagged('TargetIgnore')
	ignore[#ignore+1] = plr.Character
	local _,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
	return pos
end

function printpos()
	local mousePos = getMousePosition(mouse)
	print(mousePos.X, mousePos.Y, mousePos.Z)
end

mouse.Move:Connect(function()
	printpos()
end)
3 Likes

its printing all three axises now, until something else comes up ill mark it as a solution again. :+1:

Just so you know, you have to define a new variable for this function. You can’t just use the mouse.

local mousePos = getMousePosition(mouse)
1 Like

Seems like you already have a solution working here and from the standpoint of simplicity it’s probably your best bet.

However, I’ll take the opportunity to plug my mouse module. You can then use it like so for your purposes.

local mouse = require(game.ReplicatedStorage.Mouse).new()

function mouse:IgnoreCheck(hit, position, normal, material)
	-- return true if you want to ignore the part
	-- this is just an example:
	return hit:IsDescendantOf(hover_model) or hit.Transparency >= 1
end

Then you could use all the other aspects of the mouse you know and love with the comfort of knowing the conditions you outline will be ignored.

4 Likes