UserInputService:GetMouseLocation() does not update in RunService.RenderStepped

Currently, I am working on a dynamic crosshair that follows the mouse.

local Origin = script.Parent.Origin
local mousePos = game:GetService("UserInputService"):GetMouseLocation()

RunService.RenderStepped:Connect(function()
	Origin.Position = UDim2.fromOffset(mousePos.X, mousePos.Y)
end)

But currently, having this code will only set Origin to be where it first finds the cursor, and stays there.

I have tried using Players.LocalPlayer:GetMouse(), but I can’t seem to remove the inset that makes Y go a bit higher than its supposed to.

local mouse = Players.LocalPlayer:GetMouse()

RunService.RenderStepped:Connect(function()
	Origin.Position = UDim2.fromOffset(mousePos.X, mousePos.Y - Origin.AbsoluteSize.Y/2)
end)


image

Any idea how this can be fixed?

local Origin = script.Parent.Origin
local UserInputService = game:GetService(“UserInputService”)
local mousePos = Vector2.zero

RunService.RenderStepped:Connect(function()
mousePos = UserInputService:GetMouseLocation()
Origin.Position = UDim2.fromOffset(mousePos.X, mousePos.Y)
end)

youre getting the mouse position outside the renderstepped loop, so you only get it once and then just set the crosshair to the old position every frame

omg idk how i didnt think abt this :sob: thank you!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.