Problems with magnitude

Im trying to make it so if the player comes close to another player it prints something.
it keeps coming out in the output as: 17:48:10.892 - Workspace.vovcher.Script:7: attempt to index nil with ‘Position’
please help me.
script:

local w = workspace:GetChildren()
local char = script.Parent
local players = game:GetService("Players")
 for i,v in pairs(workspace:GetChildren()) do
	local dist = 5
	local othertorso = v:FindFirstChild("HumanoidRootPart")
	if (char.HumanoidRootPart.Position - othertorso.Position).magnitude < dist then
			local player = players:GetPlayerFromCharacter(char)
			print(player.Name)
		end
end	
1 Like

:FindFirstChild will return nil if it can’t found the part. It looks like this is a localscript in StarterCharacter so why don’t you just do char:WaitForChild(“HumanoidRootPart”) ?

its a script also it came out as this 17:55:42.948 - Workspace.vovcher.Script:7: invalid argument #1 (Vector3 expected, got Instance)

Can you show me your explorer cause I don’t understand what is “othertorso”?

local othertorso = v:FindFirstChild("HumanoidRootPart")
 for i,v in pairs(workspace:GetChildren()) do

This happens because of your Script you need to add another check to fix it here it is:

is there a way to loop it without adding while true do loops

No you will require While true do loop for it there is no other way sadly.

as stated it will return nil if the othertorso doesn’t exist as you are not doing a check for it. Doing it this way is also in general a poor approach as if you ever place things in the workspace it would be looping through everything in it instead you should loop through players.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

--- you should also want to bind this to runservice or smoething to constantly update
for i,v in pairs(game.Players:GetChildren()) do
   if v.Character then
      if v.Character:FindFirstChild("HumanoidRootPart") then
           local dist = (character.HumanoidRootPart.Position-v.Character.HumanoidRootPart.Position).magnitude
           if dist <= 5 then
               print(v.Name.." is within 5 studs")
           end
       end
   end
end

when checking character you should check if character exists and your script(assuming that’s the entire script runs before anything is even cloned into the workspace i’m assuming so it will always return nil if you’re the first one in the game.

This is sadly not going to work as the script is ServerScript Not a local script

Uh, it appears it’s in the player and in a local script. as the error shows that it’s a script in the player- so i’d assume

If it is however a script and not a localscript then you shouldn’t even try to clone the script into players and just hold everything in the server. – which this is still a bad approach as the server shouldn’t update every renderstep through a server of players compared to a localscript doing it and firing to server.

RunService runs only on localscripts mine is a script not a local script how can i use runservice on a script that isnt a localscript

Ok ive turned it into a local script and it seems that it prints my username instead of the other ones, ive added v.character ~= Character and now it doesnt print my name and it doesnt print anybody’s name near me.

Runservice can be applied both in a local and a serverscript
to use runservice in a server script you would do

local rs = game:GetService("RunService")
rs.Stepped:Connect(function(dt)
end)

however if this is a server script what i posted above will not work and you should have only 1 script in the serverscriptservice and not inserting server scripts into players.
I don’t advise it but this is how it would be done io a server:

local rs = game:GetService("RunService")

rs.Stepped:Connect(function()
	local last = nil
	for i,v in pairs(game.Players:GetChildren()) do
		if last == nil then
			last = v
		end
		if last.Character and v.Character then
			if last and last~= v then
				local dist = (last.Character.HumanoidRootPart.Position-v.Character.HumanoidRootPart.Position).magnitude
				if dist <= 5 then
					print("The player "..v.Name.." is close to "..last.Name)
				end
				last = v
			end
		end
	end
end)

Only renderstepped is local.

function Test()

end
-- these two can run on regular script.
-- RunService.Stepped:Connect(Test)
-- RunService.Heartbeat:Connect(Test)

I tested it on regular script tho.

May i ask why you dont advise it?

You can use:

Player:DistanceFromCharacter()

Player | Documentation - Roblox Creator Hub.

If it’s about character magnitude,

I could be wrong but my assumption is that it would become laggy for the server to calculate distance between players every step, but I could be wrong and if you have a small amount of players it probably wouldn’t be a problem what you do in the functions will matter though.
Anyway that is just an assumption it would probably be just as laggy for the player to check it.