Part pointing towards mouse sometimes goes backwards

Hello, yesterday I made an energy beam for my game which points towards the mouse position, but today I noticed this bug:
It works fine most of the times, but sometimes this happens and I’m not quite sure why. I’m not really experienced with this kind of things, and this is my first beam that goes towards the mouse instead of where the HumanoidRootPart is pointing. This is the code that changes the beam’s CFrame:
Beam.CFrame =,MouseHit.Position)
(MouseHit is Mouse.Hit sent with a RemoteEvent).
I thought the mouse was colliding with a part or something, but it wasn’t as I was flying as you’ve seen in the video (happens when i’m on the ground too of course).
There probably is a post somewhere with my solution, but I’m not sure of what to search. Anyone has a solution or a post that can solve this problem? Thanks.

1 Like

It goes backwards because the raycast will hit the part on the mouse. To combat this, add a FilterDescendantsInstances blacklist so that the raycast will go through the parts themselves.

local raycastParams =
raycastParams.FilterDescendantsInstances = {game.Workspace:FindFirstChild("Part")}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

If you’re using Mouse, there is:

1 Like

Alright, first thanks for the answer. Second, I tried using Mouse.TargetFilter to filter everything that’s contained in a folder in the workspace which I use to contain all the effects/moves’ parts but it doesn’t seem to be working. To filter them all I loop through workspace.Effects:GetChildren() and add all of the children to the filter, yet the problem is still there. I also tried directly adding the folder with Mouse.TargetFilter = workspace.Effects but that didn’t work too.

Does it always move in the same position?
Maybe ignore the player.

This property is essentially a single-object blacklist for mouse raycasting. For more in-depth control on raycasting, see the following functions of Workspace : FindPartOnRay , FindPartOnRayWithWhitelist and FindPartOnRayWithIgnoreList .

Edit: Try out this function which uses the newer Raycast method

local userInput = game:GetService("UserInputService")
local Mouse = {}
local RAY_DISTANCE = 1000 -- default ray distance like mouse.Hit
local cam = workspace.CurrentCamera

function Mouse.HitPosition(raycastParams, distance)
	local mousePos = userInput:GetMouseLocation()
	local viewportMouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
	local hitPosition
	if rayCastResult then
		hitPosition =  rayCastResult.Position
		hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or RAY_DISTANCE)
	return hitPosition
return Mouse

Very good that you mention this.

This is the reason why I don’t use Mouse anymore due to the restrictions.

Here’s my way to recreate :GetMouse() while using all the properties of Raycast.

local RunService = game:GetService("RunService")
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")

local filterObjects = {}

local raycastParams =
raycastParams.FilterDescendantsInstances = filterObjects
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist


	local inset = GuiService:GetGuiInset() 
	local mouseLocation = UserInputService:GetMouseLocation() - inset -- Subtract by the GUI Inset since GetMouseLocation does not take that into account and gives an inaccurate position.

	local cameraRay = game.Workspace.CurrentCamera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)
	-- Make a system here that adds filters to the raycastParam.
	local raycastResult = game.Workspace:Raycast(cameraRay.Origin, cameraRay.Direction * 50, raycastParams)

	if raycastResult then
		print("Found.") -- Do stuff here.

Oh wow never considered GuiInset I was just using the provided mouse module in AeroGameFramework thanks for that. But yeah creating a new Raycast param every render step seems excessive you might want to leave that one line out of the connection.

Anyways here are more alternatives to :GetMouse()

Or maybe make your own for max customizability.

1 Like

You’re most welcome! And oops yeah, I wrote this in a hurry and completely missed that :sweat_smile: hah, thanks for pointing that out.

Personally, I never like using modules for simple stuff (since it lets you use all of Raycast’s properties) but maybe it will help the OP.

(Fixed script example, also yeah maybe renderstepped isn’t the way to go)

1 Like

Lifesaver, thanks a lot. I’m sure I’ll use this in future projects too lol. Thanks to you too @anon24371531.