Hi, I'm a bit rusty at scripting at the moment and I'm trying to make a closest part script.The problem is that it selects all the parts and not the closest one. Here's the script:

while wait() do
	for i,v in pairs(workspace:GetChildren()) do
		if v ~= script.Parent and v ~= workspace.Baseplate and v.ClassName == script.Parent.ClassName then
			local mag = (script.Parent.Position - v.Position).magnitude
			if mag <= 50 then
				print(v.Name)
			end	
		end
	end
end

Any help is appreciated

3 comments

0

Sorry but how could I apply that to my script, i can't think straight and see what my scripts problem is

0

It is explained in the answer how the function works but you would pass it the workspace and the script.Parent.Position. It will then return the closest part and the magnitude of the position.