Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find the largest numbered child via a script?

There are many parts in a game. Each part is named a different number. How can I use a script to find the largest numbered name. I tried using an in pairs loop but couldn't find a way to find the largest numbered child. Note that the number is just the name of the part.

Is there like a function which can check which number is the highest?

1 answer

✓ Accepted answer

I'm not aware of a built-in function for this, but it's quite straight-forward when you try to code it. Here's my example script:

function GetHighestNumberedPart() ---Function To Cal
	local HighestVar = 0 --Default Number
	local PartsTable = {} --Table to insert valid parts into
	local Children_Workspace = workspace:GetChildren() --returns all children of workspace in a table
	for _, child in pairs(Children_Workspace) do --in pairs loop
		if child:IsA("Part") and child.Name ~= "Baseplate" then ---if not baseplate(Name) and if it is a part then
			table.insert(PartsTable, #PartsTable +1, child) --insert into table for valid parts(PartsTable)
		end
	end
	if #PartsTable > 0 then --If table isn't empty then
		for _,v in pairs(PartsTable) do --loop through table
			if tonumber(v.Name) > HighestVar then --tonumber to convert string to num
				HighestVar = tonumber(v.Name) ---Set new highest var
			end
		end
		return workspace:FindFirstChild(tostring(HighestVar)) --tostring to convert highestvar to num, and search for it
	end
end

local HighestPart = GetHighestNumberedPart() --Returns part
print(HighestPart.Name) --prints part name

As you can see, I've created a function which returns the part with the highest numbered name. This function uses tonumber and tostring to convert the string/number, and then returns the match in workspace for us to access.

Hope this helps!


API References:


String Article ROBLOX Developer

Table.insert And Other Stuff

2 comments

0

Thank You! It worked.

0

Np! Glad I could help

This discussion is preserved as it was. New answers, comments, and votes are closed.