Get mouse position problem

Hi! I am trying to make a part follow the player’s mouse with renderstepped(), but the part always zooms into the screen.
Here’s a video of whats happening exactly.
https://gyazo.com/4f447f7bb2ed05140a7808b02a30c8ba
image
script:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local localPlayer = Players.LocalPlayer

local part = script.Parent:WaitForChild("light"):Clone()
part.Parent = game.Workspace

local mouse = localPlayer:GetMouse()

RunService.RenderStepped:Connect(function()

	local character = localPlayer.Character
	if not character then
		part.PointLight.Enabled = false
		return
	end
	local head = character:FindFirstChild("Head")
	if not head then
		part.PointLight.Enabled = false
		return
	end

	part.PointLight.Enabled = true
	local lightPos = mouse.Hit.Position
	part.Position = lightPos
end)

1 Like

This is because every RenderStep the part is going up of itself, because that’s what mouse.Hit.Position returns. There’s a property to make .Hit and other mouse properties ignore a certain Instance called TargetFilter, you can use it with your part.

mouse.TargetFilter = part
1 Like

Sarchyx was able to solve the problem, but I have an additional suggestion.
I suggest changing your programming order.
RenderStepped is great, but technically this is after the camera positioning is done, which causes unnecessary overhead, the part will feel sluggish, behind the camera.
You should use BindToRenderStep instead, with a priority above the Enum.RenderPriority.Input, and below the Enum.RenderPriority.Camera.

2 Likes