Attempt to index number with 'X'

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
1 Like

You need to declare x as a variable using local, same with y.

local x = (Goal.X-MouseFrame.Position.X.Offset) * speed
local y = (Goal.Y-MouseFrame.Position.Y.Offset) * speed
1 Like

I already declared local x and y before the function. I still tried what you said, same problem.

RunService.RenderStepped:Connect

is the problem, this event returns the delta time, which is a number

3 Likes

Thanks for that, What I ended up doing was making the goal a local value instead of it being in the function.

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