Magnitude Multiple Parts

Magnitude Multiple Parts

Magnitude using one singe script and pairs seems to be a bit confusing.

I was wondering how I could make a gui that pops up when you get near an object. Once you leave the radius it disappears… But also if you go near a separate part then a different gui will pop up.

I have the gui’s etc… set up, but whenever use i,v in pairs to do this it glitches out because I’m to far away from the second part (in result closing the gui)

Please notice!! the following code is a test product and is very messy or non-efficient.

if active == false then
			print('isnt false, processing')
			if active == false then
				for i,v in pairs(game:GetService("Workspace"):FindFirstChild("RadiusDetections"):GetChildren()) do
					if (lpchar.HumanoidRootPart.Position - v.Position).Magnitude <= tonumber(v.Radius.Value) then
						print('activating')
						if returnvalue == false then
							active = true
							returnvalue = true
							local gui = lp.PlayerGui:WaitForChild("DialogGUI")
							gui.Main.Header.Text = tostring(v.ChatDialog["1"].Header.Value)
							gui.Main:TweenPosition(UDim2.new(0.5,0,0.895,0),"Out","Back",0.5,true)
							for i = 1, #v.ChatDialog["1"].Body.Value do
								gui.Main.Body.Text = string.sub(v.ChatDialog["1"].Body.Value,1,i)
								wait()
							end
						end
					end
					if (lpchar.HumanoidRootPart.Position - v.Position).Magnitude > tonumber(v.Radius.Value) then
						print('deactivating')
						if returnvalue == true then
							local gui = lp.PlayerGui:WaitForChild("DialogGUI")
							gui.Main:TweenPosition(UDim2.new(0.5,0,1.2,0),"In","Back",0.5,true)
							returnvalue = false
							wait(0.5)
							gui.Main.Header.Text = "Error"
							gui.Main.Body.Text = "Error"
							active = false
						end
					end
				end
			end
		end
1 Like

Just cooked this up quickly, it can be done better but just to bring an idea to the table:

local t = {
	[Vector3.new(0,1,0)] = 1,
	[Vector3.new(100,1,100)] = 2,
	[Vector3.new(200,1,200)] = 3,
	[Vector3.new(300,1,300)] = 4,
}

local Plr = game.Players.LocalPlayer
local Char = Plr.Character
local hrp = Char:WaitForChild("HumanoidRootPart")
local ClosesMagnitude, ClosesID = nil, 1

while wait() do
	ClosesMagnitude = nil
	ClosesID = 1
	for pos, id in pairs(t) do
		if not ClosesMagnitude then
			ClosesMagnitude = (hrp.Position - pos).magnitude
			ClosesID = id
		end
		if (hrp.Position - pos).magnitude <= ClosesMagnitude then
			ClosesMagnitude = (hrp.Position - pos).magnitude
			ClosesID = id
		end
	end
	print("Closes:", ClosesID, "AT:", ClosesMagnitude)
end

It’ll check the player’s position and go through the table to find the closest point the player is in and return it
like it’ll say the player is closer to #1 than any of the other positions