Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a particular code that can search efficiently of an index?

Asked by
Vezeko 2
7 years ago
Edited 7 years ago

So, in my model, there are 24 parts that are named "S1, S2, S3, S4, S5, S6, S7, S8, etc..."

I want to know if there is a way to find the certain part based on the fill/num value. For example, "1" is "S1", and "2" is "S2", and "3" is "S3", and so on and so forth.

I can always do a long version of declaring the variables and have if statements to check but there should be a short easier and efficient way of processing this. Perhaps I will need to make a library of some sort, I'm not sure.

local set = script.Parent.Fill.Value
local Level1 = script.Parent.Level1

num = 0
i = 0
if num ~= set then
repeat

    num = num + 1
    i = i + 1
    print ("Count".. i) 
    wait()

until num == set 


if num == set then --Here is where I need to find an efficient way of processing things.
    Level1.S1.Transparency = 0
    Level1.S1.CanCollide = true

end
end
script.Disabled = true

What I want it to do? To search and process by the associated number of the part's name and initiating the following effects afterwards. Which would be the Transparency and Collision change of the part.

Update: Here is the longer and tedious version of processing the index. Is not really necessary for an incrementing count , so long as the "set":

local num = script.Parent.Fill.Value

-which is now named as "num" is the value of the associated part. Basically, is directly associated to the value given for num to check.


local num = script.Parent.Fill.Value --Make sure to change it back to "set" if going to use the counting part --Library-- local S1 = script.Parent.Level1.S1 local S2 = script.Parent.Level1.S2 local S3 = script.Parent.Level1.S3 local S4 = script.Parent.Level1.S4 local S5 = script.Parent.Level1.S5 local S6 = script.Parent.Level1.S6 local S7 = script.Parent.Level1.S7 local S8 = script.Parent.Level1.S8 local S9 = script.Parent.Level1.S9 local S10 = script.Parent.Level1.S10 local S11 = script.Parent.Level1.S11 local S12 = script.Parent.Level1.S12 --num = 0 --i = 0 --if num ~= set then --repeat -- num = num + 1 --i = i + 1 --print ("Count".. i) -- wait() --until num == set --Initiate Search-- if num == 1 then print ("S1 Added!") S1.Transparency = 0 S1.CanCollide = true else if num == 2 then print ("S2 Added!") S2.Transparency = 0 S2.CanCollide = true else if num == 3 then print ("S3 Added!") S3.Transparency = 0 S3.CanCollide = true else if num == 4 then print ("S4 Added!") S4.Transparency = 0 S4.CanCollide = true else if num == 5 then print ("S5 Added!") S5.Transparency = 0 S5.CanCollide = true else if num == 6 then print ("S6 Added!") S6.Transparency = 0 S6.CanCollide = true else if num == 7 then print ("S7 Added!") S7.Transparency = 0 S7.CanCollide = true else if num == 8 then print ("S8 Added!") S8.Transparency = 0 S8.CanCollide = true else if num == 9 then print ("S9 Added!") S9.Transparency = 0 S9.CanCollide = true else if num == 10 then print ("S10 Added!") S10.Transparency = 0 S10.CanCollide = true else if num == 11 then print ("S11 Added!") S11.Transparency = 0 S11.CanCollide = true else if num == 12 then print ("S12 Added!") S12.Transparency = 0 S12.CanCollide = true end end end end end end end end end end end end --end script.Disabled = true

Still looking for a better way of shortening this code with a more efficient processing of the index. Will help save me time *and *pain. lol

Update 2: For anyone who wants to know the finished outcome of this question, here is the following code:

local set = script.Parent.Fill.Value
local MyModel = game.Workspace.A.Level1
local Data = {
    [MyModel.S1] = 1,
    [MyModel.S2] = 2,
    [MyModel.S3] = 3,
    [MyModel.S4] = 4,
    [MyModel.S5] = 5,
    [MyModel.S6] = 6,
    [MyModel.S7] = 7,
    [MyModel.S8] = 8,
    [MyModel.S9] = 9,
    [MyModel.S10] = 10,
    [MyModel.S11] = 11,
    [MyModel.S12] = 12,
}


for i,v in pairs(Data) do --"Data" is pretty much "i,v", in which "i" is MyModel.S1 and "v" is "1".
    if  v == set then --So, "v" is the number value that is used to compare the value for "set".
    print(i,v) --Checks the order in the output view window.
        i.Transparency = 0 
        i.CanCollide = true
    end
end



script.Disabled = true

Thanks for the help and information! Much appreciated!~

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

From what I can tell, it looks to me like you want to iterate through the parts of your model, check if certain conditions are met with specific parts, then run code if these conditions are true. I'm not sure how new you are to coding, so here's a few links to Wiki pages that'll come in handy.

Loops

Tables

Pairs function

Concatenation

You will also want to familiarize yourself with the :GetChildren() method

When :GetChildren() is used, it returns an array of the object's children. Once you have the array of the object's children, you can then use the array in the pairs function to iterate through the array. Then you would want to put an If statement inside the for loop so that certain conditions are met.

I won't write this for you, but I will help in every way I can to give you the tools to figure it out and learn.

The code might look something like this:

local set = script.Parent.Fill.Value
local Level1 = script.Parent.Level1

local MyModel = game.ModelsLocation.MyModel

for i,v in pairs(MyModel:GetChildren()) do
    if MyConditionIsMet == true then
        -- rest of my code here
    end
end

Remember, i is the index of the array while v is the actual value respective to that index. Since the array we're using contains the children of the object we called the method on, (in your case, the object is the model and the children are the parts within the model), then v would equal a child of the object (a part within the model). Each iteration would cycle to the next part, and since you would have an if statement inside the for loop, you could alter v's properties only if v meets a specific condition.

I hope this helps!

Don't forget to upvote and confirm if this helps answer your question or solves your problem

Ad

Answer this question