How do you get the part with the lowest y position inside a model

so i’m trying to get the part with the lowest y value in the model or inside a folder i wonder how can i achieve that

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hrp = char:WaitForChild("HumanoidRootPart")
	local humanoid = char:WaitForChild("Humanoid")
	for i,v in pairs(plr:WaitForChild("Location"):GetChildren()) do -- loops thrugh the location folder inside the player
		v.Changed:Connect(function() -- changed event to check if the player location has changed
			
			if v.Name == "FirstStage" and v.Value == true then -- check if the player is on the first stage
				
				for i,v in pairs(game.Workspace.InGameObby.Stage1:GetChildren()) do --- finding the lowest part of the first stage
					if v:IsA("BasePart") then
						
					end
				end
				
				while wait() do --- checks if the humanoidrootpart.position.y is lower than the lowest part position
					
				end
			end
			
			
		end)
	end
end)
1 Like
local model = script.Parent
local lowestpart = nil

lowestpart = model.Part -- can be any part you want, I only assigned it so it wont error by attempt to index value by nil

for k_,v in ipairs(model:GetChildren()) do
    if v:IsA("BasePart") then
          if v.Position.Y. < lowestpart.PositionY then
                  lowestpart = v
          end
    end
end

This is only a brief idea as it involves looping through a table of the model’s children, comparing their y positions

1 Like

i don’t know the lowest part in my game so how can i assign it to a variable on line3

the lowestpart can be any part you want as said in the comment on line 3
the lowestpart variable will be changed constantly as it will keep being assigned to lower and lower parts so eventually, it will refer the lowest part after the table loop

can i change the lowest part to a number value instead?

yes, you would just have to edit the script I posted a little like this.

local model = script.Parent
local lowestpart = nil

lowestpart = 3 -- can be any number you want

for k_,v in ipairs(model:GetChildren()) do
    if v:IsA("BasePart") then
          if v.Position.Y < lowestpart.PositionY then
                  lowestpart = v.Position.Y -- lowestpart is assigned to the y value of the vector3 position
          end
    end
end```

If you won't refer to a part but rather a number, you can use what I just posted
1 Like