I’m making a UI cursor that if the player is close enough to a tagged part, it will lock the cursor on it.
I’ve tried checking if it is nil, but the same error still happens.
This is a block of code, if you want to test it for yourself, please tell me and I will send you the whole thing.
local function UpdatePosition(Goal)
if not IsMoving then
IsMoving = true
RunConnect = RunService.RenderStepped:Connect(UpdatePosition)
end
x = (Goal.X-MouseFrame.Position.X.Offset) * speed --this is where the error is(attempt to index number with 'X')
y = (Goal.Y-MouseFrame.Position.Y.Offset) * speed
MousePastPosition = MouseFrame.Position
MouseFrame.Position = UDim2.fromOffset(MouseFrame.Position.X.Offset+x,MouseFrame.Position.Y.Offset+y)
MouseMagnitude = math.clamp(Vector2.new(MouseFrame.Position.X.Offset-MousePastPosition.X.Offset,(MouseFrame.Position.Y.Offset-MousePastPosition.Y.Offset)).Magnitude,-150,150)
MouseRange = ((MouseMagnitude+150)/50)-2
UIAspectRatioConstraint.AspectRatio = MouseRange
MouseFrame.Rotation = math.atan2(MouseFrame.Position.Y.Offset-MousePastPosition.Y.Offset,MouseFrame.Position.X.Offset-MousePastPosition.X.Offset) * 180 / math.pi
if MouseRange == 1 then
IsMoving = false
RunConnect:Disconnect()
end
end
Heres the part that checks if the player is close enough:
local function ActivateTool(Tool)
if not Tool.Handle then return end
while task.wait() do
CurrentMagnitude = (Character.HumanoidRootPart.Position - Tool.Handle.Position).Magnitude
if CurrentMagnitude ~= PastMagnitude then
PastMagnitude = CurrentMagnitude
if CurrentMagnitude < 10 then
Highlight.Adornee = Tool
UpdatePosition(Workspace.CurrentCamera:WorldToScreenPoint(Tool.Handle.Position))
else
if Highlight.Adornee then
Highlight.Adornee = nil
UpdatePosition(Workspace.CurrentCamera.ViewportSize/2)
end
end
end
end
end