Attempt to index number with 'Magnitude'?

Ted here. I’m trying to define a variable called, ‘distance’ which is defined as the magnitude of the difference of the player’s head position and the crafting bench model’s position.

I keep getting error:
image

Code:

--//player walks up to crafting bench model, crafting ui should appear visible to the player. if they walk away, it goes invisible
--//need to constantly check player's magnitude.
--//Need to evaluate if player's magnitude is less than 2 (magnitude btetweeen themself and the craftingbench), if so display the crafting interface, else make it invisible

local RunService = game:GetService('RunService')
local HTTPService = game:GetService('HttpService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Player = game.Players.LocalPlayer
local Interface = script.Parent
local CraftingBench = workspace.CraftingBench
local CraftingMenu = Interface.Windows.Crafting

CraftingMenu.Visible = false

task.defer(function()
	while task.wait(0.25) do
		if Player then
			if Player.Character then
				local headPosition = Player.Character.Head.Position
				local craftbenchPosition = CraftingBench.MeshPart.Position
				
				local distance = Player:DistanceFromCharacter(headPosition - craftbenchPosition).Magnitude
				if distance <= 5 then CraftingMenu.Visible = true else CraftingMenu.Visible = false end --**displays crafting window false/true on distance
			end
		end
	end
end)

Try this:

local distance = (headPosition - craftbenchPosition).magnitude
1 Like

DistanceFromCharacter returns a number, not a Vector3. In this case distance is already the distance you need.

Alternatively, you could get rid of DistanceFromCharacter, and use magnitude normally.

local dist = (headPosition - craftbenchPosition).Magnitude