Custom mouse handler with raycasting issues

For my game, I use a custom mouse handler that uses raycasting to simulate the normal Roblox mouse object, with some enhancements. Recently, I refactored it to use the new raycasting. The only issue is I can’t figure out another efficient way of blacklisting transparent parts. Emphasis on the efficient part, but it has to run every frame.

Here’s what I have now:

RunService:BindToRenderStep("MouseUpdate", 1, function()
	local position = Mouse:GetPosition()
	local unitRay = camera:ViewportPointToRay(position.X, position.Y)
	raycastParams.FilterDescendantsInstances = Funcs.ConcatTables(Mouse.TargetFilter, getTransparentParts())
	local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * lengthCap, raycastParams)
	if raycastResult then
		Mouse.Target = raycastResult.Instance
		Mouse.Hit =
		Mouse.HitNormal = raycastResult.Normal

In the Funcs.ConcatTables(Mouse.TargetFilter, getInvalidParts() part of the code, ideally you could have the function of getTransparentParts() return a table of all transparent parts, but that obviously isn’t efficient. What should I do here?

I mean, couldn’t you just grab a table of all transparent parts and then never again? Or would this be dynamically changing?

Another idea: You could possibly have the server parent all transparent parts in a separate model, and you can input that model in the ignore list directly.

Well, ideally, the server wouldn’t have to parent all transparent parts to a model. To go with that, the amount of transparent parts is dynamic so you would have to recheck at some point. Possibly whenever an instance is created?

If you’re looking at efficiency, that’s the most efficient way I’m aware of.

Alternate method could be using CollectionService to tag all transparent parts, and then, like you said, using a .ChildAdded event on Workspace to detect when new transparent parts have been added. Then you could either use CollectionService to tag that new object or just parent it to a model.

What if you just used CollectionService entirely? CollectionService does have a GetInstanceAddedSignal event that you could listen to and add to a growing list of transparent parts.

That could work too. You would still need to determine when a new object was added to workspace to tag it though, unless you’re cloning the same object, which in that case I would think existing tags transfer.

Well, if I was dynamically adding objects/models into the workspace, then I could create a function to run though the model and find any transparent tags and to the solution described below.

Maybe a more ideal system to accompany this possible problem is to add a tag the remove it. When the tag is removed, you could listen to GetInstanceRemovedSignal to grab the part and add it to a table. This would leave no extra tags, just create one then pretty much instantly remove it. Not sure of the performance impact of such an activity, especially when inserting larger objects/models.

You might be too concerned about over-optimizing. I highly doubt there’d be any significance to performance whatsoever regardless of which method you select.

1 Like