Get the last Part out of a Folder

I am currently learning scripting in Roblox and for a simple start I am creating an Obby.
My current problem is that I want to display a GIU on the top of the Screen that shows to player the progress in percent. The GIU is done and it works when set the current max number of Checkpoints as a value in the calculation, but for learning Lua and future proof I want the Calculation to not use a set number I always need to change when I add new Stages but I want it to just grab the last Checkpoint in my Checkpoints Folder (All the names of the Checkpoints are just numbers, so the first Checkpoint is just named “1” and the second “2” and so on.) and put that in the calculation.
How can i do that?

My current code is

local CPoints = game.Workspace.Room1.Checkpoints

local CPointsTable = {CPoints:GetChildren()}
table.sort(CPointsTable)
print(CPointsTable)
local points = CPoints:GetChildren();
local lastPart = points[#points]
1 Like

Thank you for your reply. I think my problem now is, that the Game grabs the Instance name, so a String and so the calculation doesn’t work.

Depending on what the string looks like, you might be able to use

tonumber(targetValue)

Reference:

When I do that, I get an error that says that I attempted to “perfrom arithmetic on number and nil”

If all your checkpoints are just number then you can just convert the last checkpoint number to a string and index it like this:

local CPoints = game.Workspace.Room1.Checkpoints

local Children = CPoints:GetChildren()
local lastCheckpoint = Children[#Children..""]

I get the same Error “attempt to perfrom arithmetic on number and nil”

Can I see you current code?


I made a mistake, the :GetChildren() function returns an array not a dictionary so the it should be something like this instead:

local CPoints = game.Workspace.Room1.Checkpoints

local ChildrenNum = #CPoints:GetChildren()
local lastCheckpoint = CPoints[ChildrenNum..""]

tell me if that works

Of course

local CPoints = game.Workspace.Room1.Checkpoints
local Stage = game.Players.LocalPlayer.leaderstats.Stage.Value



local CPointsChilds = CPoints:GetChildren();
local lastpart = CPointsChilds[#CPointsChilds..""]

local Result = (Stage * 100)/lastpart
Label.Text = Result

This is the folder
Roblox

If you are trying to get the number of the last part which for the picture above would be the number 3, you can just use the number of children in the checkpoints folder which can be done like this:

local CPoints = game.Workspace.Room1.Checkpoints
local Stage = game.Players.LocalPlayer.leaderstats.Stage.Value



local CPointsChilds = CPoints:GetChildren();
local lastpart = #CPointsChilds

local Result = (Stage * 100)/lastpart
Label.Text = Result
1 Like

Yep, that works. Thanks a lot.

No problem

1 Like