Help with finding the highest elevation on a map using raycasting

Hey developers, I was working on a script that was able to find the highest elevation on a map a couple years ago with a friend (he basically made the whole script). Shortly afterward I had lost contact with the friend, so I have no idea how to fix the script. At the time I knew nothing about vector math (I just started learning it a few days ago) and also just found a use for the script. If anybody could tell me how to fix the script (and maybe why that fixes it), that would be very appreciated, I would like to stick with the rays to practice my skills with vectors (if possible). I do realize the code below is was made to find the lowest elevation on a map, but my friend said something about how I could use that information to find the highest elevation, so I’d like to stick with that as well (if possible).

I recently updated it to use the newer way of raycasting

The script no longer times out

function elevation()
	local lowest = -math.huge
	local base = workspace:FindFirstChild("Baseplate")
	if(not base)then
		base = Instance.new("Part")
		base.Name = "Baseplate"
		base.Anchored = true
		base.CanCollide = false
		base.Size = Vector3.new(512, 20, 512)
		base.Position = Vector3.new()
		-- Just creates a new baseplate if there isn't already a baseplate
	end
	for x = 1, base.Size.X do
		for z = 1, base.Size.z do
			local rayCastParams = RaycastParams.new()
			rayCastParams.FilterDescendantsInstances = {base}
			rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
			local rayOrigin = Vector3.new((base.Position.X+(base.Size.X/2)+x), 10000, (base.Position.Z+(base.Size.Z/2)+z))
			local rayDirection = (rayOrigin-(rayOrigin-Vector3.new(0,-10000,0))).unit*10000
			local params = {rayOrigin, rayDirection, rayCastParams}
			local cast = workspace:Raycast(unpack(params))
			if(cast)then
				local part = cast.Instance
				local pos = part.Position
				local mag = (rayOrigin-pos).magnitude
				if(mag<lowest)then
					lowest = mag
				end
			end
		end
	end
	print(lowest)
end

I forgot to add that I’m getting an error at the line that reads lowest = mag

[20:33:53.436 - Workspace.Something:60: Script timeout: exhausted allowed execution time]

Try this:

function elevation()
	local lowest = -math.huge
	local base = workspace:FindFirstChild("Baseplate")
	if(not base)then
		base = Instance.new("Part")
		base.Name = "Baseplate"
		base.Anchored = true
		base.CanCollide = false
		base.Size = Vector3.new(512, 20, 512)
		base.Position = Vector3.new()
		-- Just creates a new baseplate if there isn't already a baseplate
	end
	for x = 1, base.Size.X do
		for z = 1, base.Size.z do
			wait() --for avoid timeouts
			local rayCastParams = RaycastParams.new()
			rayCastParams.FilterDescendantsInstances = {base}
			rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
			local rayOrigin = Vector3.new((base.Position.X+(base.Size.X/2)+x), 10000, (base.Position.Z+(base.Size.Z/2)+z))
			local rayDirection = (rayOrigin-(rayOrigin-Vector3.new(0,-10000,0))).unit*10000
			local params = {rayOrigin, rayDirection, rayCastParams}
			local cast = workspace:Raycast(unpack(params))
			if(cast)then
				local part = cast.Instance
				local pos = part.Position
				local mag = (rayOrigin-pos).magnitude
				if(mag<lowest)then
					lowest = mag
				end
			end
		end
	end
	print(lowest)
end
1 Like

This stops the script from timing out, although it doesn’t find the highest elevation on the map

I don’t think you should use raycast
Just search with the Y-axis

local HighestPart = nil

for _,Part in pairs(game.Workspace:GetDescendants()) do
	if Part:IsA("BasePart") and Part:IsA("Terrain") == false then
		if HighestPart == nil then
			HighestPart = Part
		end
		if HighestPart.Position.Y < Part.Position.Y then
			HighestPart = Part
		end
	end
end

print(HighestPart:GetFullName())

I’m aware that raycasting would be overcomplicating things, although I’d like to use it to gain some more experience with vectors and rays. Thanks for your suggestion though.