Magnitude always returning 0 instead of retrieving the distance

For some reason this function doesn’t want to tell me the magnitude in play solo mode. I haven’t tested whether or not it works in the actual game yet.

local function chaseTarget(MobNpc, MobRoot, Humanoid, Target, TargetRoot)
	if (TargetRoot.Position - MobRoot.Position).Magnitude > 5 then
		Humanoid:MoveTo(TargetRoot.Position)
	else
		print("Npcs close to eachother")
	end
	print((TargetRoot.Position - MobRoot.Position).Magnitude)
end

Can you provide the entire script? Clearly the magnitude check itself is not the problem.

I thought so too, I printed the root and the positions and it printed correctly in the function. But when I print the magnitude it says 0

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Storage = ReplicatedStorage.Storage
local Remotes = Storage.Remotes

local HotkeyPress = Remotes.HotkeyPress
local HotkeyReceive = Remotes.HotkeyReceive
local Living = workspace.Alive

local SkillsFolder = game.ServerStorage.Skills

local function lookForTarget(MobNpc, MobRoot)
	local closestTarget, closestTargetRoot
	for _, indexMob in ipairs(Living:GetDescendants()) do
		local indexRoot = indexMob:FindFirstChild("HumanoidRootPart")
		if indexRoot then
			if closestTarget and closestTargetRoot then
				if (indexRoot.Position - MobRoot.Position).Magnitude < (closestTargetRoot.Position - MobRoot.Position).Magnitude then
					closestTarget = indexMob
					closestTargetRoot = indexRoot
				end
			else
				closestTarget = indexMob
				closestTargetRoot = indexRoot
			end
		end
	end
	return closestTarget, closestTargetRoot
end

local function chaseTarget(MobNpc, MobRoot, Humanoid, Target, TargetRoot)
	if (TargetRoot.Position - MobRoot.Position).Magnitude > 5 then
		Humanoid:MoveTo(TargetRoot.Position)
	else
		print("Npcs close to eachother")
	end
	print((TargetRoot.Position - MobRoot.Position).Magnitude)
end

while task.wait() do
	local NpcTable = Living:GetDescendants()
	for _, MobNpc in ipairs(NpcTable) do
		task.spawn(function()
			if MobNpc:FindFirstChild("HumanoidRootPart") and MobNpc:FindFirstChild("Humanoid") then
				local MobNpcTarget, MobNpcTargetRoot
				local Configuration = MobNpc:FindFirstChild("Configuration")
				if Configuration then
					MobNpcTarget = Configuration.Target.Value
					if MobNpcTarget and MobNpcTarget.Parent then
						MobNpcTargetRoot = MobNpcTarget:FindFirstChild("HumanoidRootPart")
					else
						if Configuration.Target.Value ~= nil then
							Configuration.Target.Value = nil
						end
					end
				end
				local MobRoot = MobNpc.HumanoidRootPart
				local Humanoid = MobNpc.Humanoid
				local Target, TargetRoot = lookForTarget(MobNpc, MobRoot)
				if not Target then
					if MobNpcTarget and MobNpcTarget.Parent and MobRoot then
						Target = MobNpcTarget
						TargetRoot = MobNpcTargetRoot
					end
				end
				if Target and Target.Parent and TargetRoot then
					if (TargetRoot.Position - MobRoot.Position).Magnitude < 50 then
						if Configuration then
							Configuration.Target.Value = Target
						end
						local Magnitude = (MobRoot.Position - TargetRoot.Position).Magnitude
						print(Magnitude)
						chaseTarget(MobNpc, MobRoot, Humanoid, Target, TargetRoot)
					else
						if Configuration then
							Configuration.Target.Value = nil
						end
					end
				end
			end
		end)
	end
end

Try this instead:

local function chaseTarget(MobNpc, MobRoot, Humanoid, Target, TargetRoot)
	if (TargetRoot.Position - MobRoot.Position).Magnitude > 5 then
		Humanoid:MoveTo(TargetRoot.Position)
	else
		print("Npcs close to eachother")
	end
	print(TargetRoot.Position, " - ", MobRoot.Position)
end

That’ll show you what each value is instead of just printing the Magnitude so you can see which of the values isn’t what it should be.

1 Like

Yeah I think I just realized the problem haha I forgot to exclude the specific Npc from the loop so that it doesn’t scan itself as a target.

1 Like

Nice!
I keep finding people say ‘just put a print statement in there’ but if the print statement doesn’t tell you what the values are then it only lets you know the script got to that section.

1 Like

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