Help with Magnitude

Hey developers, I am attempting to make a part where if you are in a certain area of said part it would print('Welcome').

--local script

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local HRP = character:WaitForChild('HumanoidRootPart')
local detection = script.Parent.Parent.Detection
local mag = (detection.Position - HRP.Position).Magnitude

if mag <= 10 then
	print('Welcome')
end

Hierarchy:

image

No errors in the output, it just doesn’t fire.

--server script (just an attempt)
local part = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local mag = (part.Position - char.HumanoidRootPart.Position).Magnitude
		
		if mag <= 10 then
			print('Welcome')
		end
	end)
end)

Honestly, I’m clueless, first time using magnitude,
any tips, suggestions or solutions would be greatly appreciated.

Cheers

I think this is happening because you put this in a character added event. If I’m not mistaken, whatever is in a character added event will run when the character loads, then won’t run again. So, the character is spawning, the script sees that the character isn’t in the range of the part, and the if statement never runs again. Try using a while loop that will constantly check if the character’s magnitude from the part is lower than 10.

Once again, I’m not sure if this is the reason that your problem is happening, but please let me know if my idea works.

Thanks for reading this and I hope this helps!

Edit: your code can look like this in a local script:

while wait(0.5) do
   if (game.players.localplayer.character:WaitForChild(“HumanoidRootPart”).Position - part.Position).Magnitude <= 10 then
      print(“Yay, it works!”)
   end
end

P.S I was kind of lazy writing this, so put some “WaitForChilds” in there

I dont think it is good practice to add while wait()

2 Likes

So I assume you’re trying to continually see whether a player is in range of the part or not? If so, here’s a solution.

Place this code in a script inside of the part:

-- Services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Variables
local part = script.Parent

RunService.Heartbeat:Connect()
    for _, player in pairs(Players:GetPlayers()) do
        playerPosition = player.Character.HumanoidRootPart.Position
        if (part.Position - playerPosition).Magnitude <= 10 then
            print("Welcome")
        end
    end
end

Here’s a breakdown. The code will:

  1. Run the code every frame (which may be every 1/30th of a second, or higher such as 1/60th)
  2. Loops through all the players in the game
  3. Checks if each player is within a 10 stud distance of part
  4. If they are, then it will print “Welcome”
  5. If they are not, then it will continue to cycle through all the players

Also, I made a post on a formula I made, showing how .Magnitude works (pretty simple). If you’d like to see it, here it is: How does magnitude and range work? - #6 by MJTFreeTime

Hope I helped!

1 Like

If you need help, keep in mind there is a Developer hub, where you can get info on instances.

And if i’m not wrong, you should call it on a server script.

Another problem I found is that this is being called when a player joins.
So unless you want it to make sure a player is spawning in the correct location…

–server script
local part = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
pchar = char --Var
end)
end)

while true do --This will run the code every second.
wait(1) --Needed to stop studio from crashing lol.
local mag = (part.Position - pchar.HumanoidRootPart.Position).Magnitude --Executing that Var.
if mag <= 10 then --Checking distance.
print(‘Welcome’) --Doing whatever.
end
end

Doing this is very impractical though, and you should just use ontouch.

This is the way I would do it; with the Heartbeat event on the server.
Just be sure to properly connect the event to a function (You’ve left the :Connect() statement empty)
So that part would instead be:

RunService.Heartbeat:Connect(function()
    for _, player in pairs(Players:GetPlayers()) do
        playerPosition = player.Character.HumanoidRootPart.Position
        if (part.Position - playerPosition).Magnitude <= 10 then
            print("Welcome")
        end
    end
end)
2 Likes

I didn’t think it was best, either. I was going to use RunService for the issue, but I used a while loop because it was easier to write out.

Anyways, thanks!

1 Like

Yeah, nor problem. Have a nice day!

1 Like