Flashlight effect doesn't work

Hi! I want to create the flashlight effect like in windows 10 Снимок экрана 2021-11-07 200025

This work incorrect

plr = game.Players.LocalPlayer
mouse = plr:GetMouse()
fe = script.parent

while true do
fe.Position = Udim2.new(mouse.X, mouse.Y)

Udim2.new() uses four arguments (X.Scale,X.Offset,Y.Scale,Y.Offset), so you should use UDim2.fromOffset that will match your situation or just try Udim2.new(0, mouse.X, 0, mouse.Y).

Also, i think you would want it to update every frame, so instead of while loop you can use RunService.RenderStepped event like:

    fe.Position = Udim2.fromOffset(mouse.X, mouse.Y)

Hope this helps!

1 Like

Actually I would use mouse.Move:Connect as it’ll be a lot more performant to update the position only when the player moves the mouse and not every frame. And u don’t get any stuttering either

local players = game:GetService("Players")
local plr = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = plr:GetMouse()
local fe = script.parent

	local mousePos = mouse.Hit.p --gets mouse position as a Vector3 value
	local mousePos2 = mouse.Hit -- gets mouse position as a CFrame value

As the previous post suggested make use of the “Moved” event belonging to the mouse instance which is fired every time the mouse is move, I’ve also added 2 different ways in which the cursor’s current location can be ascertained. On an added note, you should declare all variables as locals.

Local variables are obtained faster than global variables because they’re integrated into the environment in which they were created. If possible, you should always use local variables over global variables, unless there’s a specific reason otherwise.