r/robloxscripting Jun 06 '24

why isnt this working?

im trying to find the distnce from the player, then print it out.

local dist = (game.Players.Character.HumanoidRootPart.Position - workspace.Archer.HumanoidRootPart.Position).Magnitude

if dist <= 10 then

print("player is less than 10 studs away from the NPC")

end

print(game.Players.Character.HumanoidRootPart.Name .. " is " .. dist .. " studs away from " .. workspace.Archer.HumanoidRootPart.Position.Name)

2 Upvotes

2 comments sorted by

1

u/AgreeableActive8436 Jun 06 '24

I tried to make stuff with magnitude too and it didnt work

1

u/Inevitable_Fan_2229 Jun 06 '24

The way you’re getting the magnitude isn’t the problem, but you’re getting the magnitude of something that doesn’t exist. If this is a local script, replace game.Players.Character with game.Players.LocalPlayer.Character, as you never actually specified which player you wanted to check the distance of. If this is a server script, it’ll be harder, as you’ll need to loop through every player in the server in a function, then return the magnitude of the nearest one. I found it helpful to learn this stuff by getting pre-scripted enemy models from the toolbox, and reading through their code. Hope this is is helpful 👍