How to get an index's name on a table

Hello,
I am unsure if the title helps or not, but I am looking through folders by using ipairs/pairs. However, this is filled with RNG, so i’m using :GetChildren() to call for names. Unfortunately since it gets put into a table, I actually don’t know how to retrieve the name, only the index. (I am not good with tables).

Here’s the code that I am talking about:

rakeHumanoid.HealthChanged:Connect(function()
	if rakeHumanoid.MaxHealth >= 10 and not startedHour.Value then
		local chosenMode
		local folderToChose
		for h, v in pairs(hours:GetChildren()) do
			local shouldStartHour = math.random(1, 1)
			if shouldStartHour == 1 then
				local randomNumber = math.random(0, 200)
				if randomNumber >= v.Chances.MinValue and randomNumber <= v.Chances.MaxValue then
					if v.Name ~= "Chances" then
						local okay = v:GetChildren()
						print(okay)
						startedHour.Value = true
					end
				end
			end
		end
	end
end)

The code above prints this in the output:
image
What the number equals is what I want to call to a variable.

Any help is appreciated!

3 Likes

So what just Table[number here]

What I believe you are trying to do is that you have a Folder of different “hours” that have an instance structure like HoursFolder.<Hour>.(<HourName>+Chances). In this case, because these are objects, it would actually be kind of finicky to get the name of the hour through an Instance through it’s name if it’s name is different for every hour.

Instead, you should change the structure so that HourName (in this case the Instance named “Pirate Hour”) is a StringValue with the name “HourName” and the .Value property set to “Pirate Hour” (or what have you), allowing you to get the name of the hour with:

local hourName = v.HourName.Value

Or, even better, use Attributes, so that you can simplify the code to:

local hourName = v:GetAttribute("HourName")

You can simplify this to:

for h, v in hours:GetChildren() do

It is functionally the same as wrapping it in a pairs() call.

1 Like

Hey there! Thank you for the tip on using Attributes (I know how handy those can be). But yes, your hierarchy tree is very similar to how you explained it. This is what it looks like in the Explorer:
image
It put this in the output after taking your advice:
image
As you can see in the screenshot above, it starts off just displaying what the children for the hours folder is - and it isn’t a table. However, after looking into v (the folders in Hours) it goes back to being a table. I am trying to avoid this so I can actually call certain things in that folder.

2 Likes

Ohhh, I didn’t realize that you had different difficulties.

The one about pairs? It shouldn’t change how the Instances are accessed and what v is, it should remain exactly the same. The only thing that can explain v changing is that you’ve changed the hours variable.


I’m having trouble understand exactly what you want the code to do. Do each of these hours have different requirements and you’ve just separated them into folders? I wouldn’t really recommend that because then you won’t be able to easily find all the hours. Instead, you could give them a Difficulty attribute and then configure how likely each of the difficulties are to be enabled in some sort of “configuration” object or ModuleScript.

Then, you can change the code to initially a dice to select the difficultly, and then go through each of the scenarios and find the first one that meets all the requirements (i.e. has the same difficulty, + other variables like maybe an hour can only trigger in Halloween).

But another possibility has come across my head that maybe you want some hours of the day to be difficult (eg: midnight has “hard” mode) and there’s a random chance for each of the scenarios in that difficultly group to be chosen? As I said before, sometimes just seeing the related code and instance hierarchy isn’t enough - you should include what the code should be doing as well, otherwise it can be difficult to give helpful or useful advice.

1 Like

Apologies for not doing an in-depth on what I want to achieve - I have only made so much topics so I am quite brand new to this.

To answer your question, the code is supposed to go into the children of the folder “hours”, which includes three folders, “Common”, “Rare”, and “Hard” (We will call them difficultyFolders) The reason why I want to look through difficultyFolders is because they include things that I need inside of them.

For instance, inside of the difficultyFolders they all have a IntConstrainedValue called “Chances”, and the name of the those different difficulties that difficultyFolder has. Since I’ve called all of those folders with :GetChildren(), I can grab all of their “Chances” variable and make a math.random() number that is inbetween the chance’s MinValue and MaxValue. If it’s inbetween one of the difficultyFolders chances, then it will select that one difficultyFolder.
image
Lets say it got the difficultyFolder, “Hard”, so now I only view the contents inside of Hard. I then do a math.random() with the amount of children Hard has. It luckily hits “Amargeddon”.
image
In Amargeddon it includes a script called Main, and a “Rewards” folder. All I want to do is Enable the Main script - that’s it. But unfortunately, after looking into the difficultyFolder, it turns into a table so I cannot enable the Main script nor can I call the Rewards folder making it completely obsolete and erroring. I want to avoid that.

Apologies if this still doesn’t make much sense; If others don’t understand this then of course it’s better if I try a different approach. Nonetheless, I’ll look into ModuleScripts to see if it will make anything easier.

If I’m understanding this correctly, can’t you just do v.Main.Enabled = true?