.magnitude is not working

So I am making a test script just to make sure that my script works (I was going to make slender man-like movement, but first I’m making the character move to the player) but for some reason there’s no errors but nothing is happening.

I’ve tried to unanchor the character (which did not do anything), and tried to just print. I changed it though because it didn’t work. Here is my script:

local player = game.Players.LocalPlayer

local hecklebobm = script.Parent

local Torso = hecklebobm:WaitForChild("Torso")

local Humanoid = hecklebobm:WaitForChild("Humanoid")

local function findTarget()
	local DISTANCE = 500
	
	local target
	
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		if human and torso and v ~= script.Parent then
			if Torso.Position - torso.Position.magnitude < DISTANCE then
				DISTANCE = (Torso.Position - torso.Position).magnitude
				target = torso
			end
		end
	end
	return target
end

while wait(1) do
	local torso = findTarget()
	if torso then
		Humanoid:MoveTo(torso.Position)
	end
end

I followed this off a tutorial, so if it looks familiar that’s why. And yes, the character has a humanoid. I’ve tried 2 different plugins that insert players but they just do the same thing. No errors, doesn’t move to the player though.

Anything is appreciated.

Instead of writing

if Torso.Position - torso.Position.magnitude < DISTANCE then

You need to wrap it in ()'s

if (Torso.Position - torso.Position).magnitude < DISTANCE then

Doesn’t work. Still lifeless like it already looks :confused:

Does anything appear in the output or no? You said there was no error but the line where you have no ()'s should’ve shot off an error for trying to subtract a number from a Vector3. Maybe the if statement conditions aren’t satisfied so it doesn’t return anything.

Nope. Nothing in the output sadly

Try printing things in the findTarget function to see if its running as planned.

Didn’t print a single thing. just the game making an auto recovery file or something

Where is the script located? Is it disabled?

Are you sure your if statements are all getting through correctly?
(What i mean by this, is if it returns false on any of them you’re not getting any response, which i think you should be for error handling)

I followed a tutorial, it worked for the person making the video. Heres what I followed: Advanced Roblox Scripting Tutorial #22 - Magnitude (Distance) [Beginner to Pro 2019] - YouTube

change every single .magnitude to .Magnitude

Are you trying to get the character to follow the player?
Also, are you doing this on a server script or a local script? It seems you are using a local script to do this.

Yes. Yea I was using a local script. Should I make it server sided?

yes, it should be a server script as used in the video you sent.

1 Like

It should be done on the server, you don’t need the player variable because the local player property is useless when used on the server, only useful when on the client

1 Like

Works. Thanks, appreciate your help guys :slight_smile:

1 Like

Btw, here is a small improvement for you, you can learn pathfinding here: Move AI Bots w/ Pathfinding! - 2019 Scripting Tutorial (NPC Character Movement) - YouTube

local hecklebobm = script.Parent
local Torso = hecklebobm:WaitForChild("Torso")
local Humanoid = hecklebobm:WaitForChild("Humanoid")

function findTarget()
	local DISTANCE = 500
	local target
	
	for i, v in pairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local TargetTorso = v:FindFirstChild("Torso")
		if human and TargetTorso and v ~= hecklebobm then
			if (Torso.Position - TargetTorso.Position).Magnitude <= DISTANCE then
				DISTANCE = (Torso.Position - TargetTorso.Position).Magnitude
				target = TargetTorso
				return target or TargetTorso
			end
		end
	end
	
	return nil
end

task.spawn(function()
	while wait(1) do
		local torso = findTarget()
		if torso then
			Humanoid:MoveTo(torso.Position)
		end
	end
end)

It was just a test script. I’m trying to make slender man movement for it, but thanks though!

1 Like