How would I find the highest NumberValue with no previously assigned table?

I was making Sheer Heart Attack so I made a serverscript that adds temperature to everything. Onto the script for sha I was going to try and and find see which one had the highest temperature and it goes to that but I had no idea how to loop through which one is the highest.

The best post I found was for this How to check which of the given numbervalues have the highest number? but this one is for stuff thats already in a table.

How would I make this?

while task.wait(1) do
	for i, v in pairs(game.Workspace:GetDescendants()) do
		local mag = (v.Position - script.Parent.HumanoidRootPart.Position).Magnitude
		if mag < 30 then
			if v:IsA("BasePart") then
				if v:FindFirstChild("Temperature", true) and not v:GetAttribute("hurtbox", true) then
					local temp = v:FindFirstChild("Temperature", true)
--where stuff goes
					script.Parent:MoveTo() --what i was planning to do once found the highest temp one
				end
			end
		end
	end
end
1 Like
Testing
local model = Instance.new("Model", workspace)
model.Name = "TempTest"

math.randomseed(tick())
for i=1,5 do
	local p=Instance.new("Part")
	p.Size=Vector3.new(2,2,2)
	p.Position=Vector3.new(i*6,3,0)
	p.Anchored=true
	p.Parent=model
	local t=Instance.new("NumberValue",p)
	t.Name="Temperature"
	t.Value=math.random(1,100)
end

task.wait(1)

local highest, target = -math.huge, nil
for _,v in pairs(model:GetDescendants()) do
	if v:IsA("BasePart") and v:FindFirstChild("Temperature") then
		local val=v.Temperature.Value
		if val>highest then
			highest,target=val,v
		end
	end
end

for _,v in pairs(model:GetDescendants()) do
	if v:IsA("BasePart") and v:FindFirstChild("Temperature") then
		print(v.Name, v.Temperature.Value)
	end
end

if target then
	print("Highest:", target.Name, target.Temperature.Value)
end
Applied
while task.wait(1) do
	local highest, target = -math.huge, nil
	for _, v in pairs(workspace:GetDescendants()) do
		if v:IsA("BasePart") and v:FindFirstChild("Temperature", true) and not v:GetAttribute("hurtbox") then
			local mag = (v.Position - script.Parent.HumanoidRootPart.Position).Magnitude
			if mag < 30 then
				local temp = v.Temperature
				if temp.Value > highest then
					highest, target = temp.Value, v
				end
			end
		end
	end
	if target then
		script.Parent:MoveTo(target.Position)
	end
end
1 Like

Is there a smoother way I could apply MoveTo()?

1 Like

Well there is HumanoidRootPart:PivotTo(CFrame.new(target.Position))
You’ll have to test that.. I was focused on that math.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.