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
wait(0.1)
fe.Position = Udim2.new(mouse.X, mouse.Y)
end

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:

game:GetService("RunService").RenderStepped:Connect(function()
    fe.Position = Udim2.fromOffset(mouse.X, mouse.Y)
end)

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

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

mouse.Moved:Connect(function()
	local mousePos = mouse.Hit.p --gets mouse position as a Vector3 value
	local mousePos2 = mouse.Hit -- gets mouse position as a CFrame value
end)

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.