Select a specific parent with script

So I want to make a script that does a formula automatically using a parent name.
image

As you can see there’s multiple models with names from 1 to 9. I have one script located in each of these models and I want it to select the Parent name using tonumber(Part.Name). I have this for now:

local Cost = tonumber(script.Parent.Parent["1"].Name)
local Amount = tonumber(script.Parent.Parent["1"].Name)

The problem is that I don’t want to always change the [“1”] to the model name, I want it to be automatic… So is there a way to select automatically the Parent of where the script is, like only the model of where the script is located without having to change something everytime since it needs to be automatic.

Thanks

1 Like

Im not sure what youre asking, are you asking how to automate where to parent things? If you are, you can just use a for loop

local costs = {}

for index, model in script.Parent.Parent:GetChildren() do
    table.insert(costs, model.Name)
end

And later you can index these costs from the table, or you can create a dictionary

1 Like

I don’t think that’s what i’m looking for. What I want is to “extract” a Parent name automatically:
When I type script.Parent.Parent. it shows me multiple things:
image

In this case, it shows me all the models from 1 to 9. But I don’t want to type script.Parent.Parent["1"].Name for the first script, then to change this to script.Parent.Parent["2"].Name because I have multiple scripts that I need to duplicate hundreds of times.

1 Like