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

Finding a certain value in 2 models with the same name?

Asked by 8 years ago

In my game, I have two models with the same name. Each of them have a value, one of them is correct and one of them is wrong. I also have a text button in startergui called Initiate, which when clicked is supposed to find the model with the string value of "correct." Is there a way to do this?

2 answers

Log in to vote
1
Answered by
davness 376 Moderation Voter
8 years ago

Yes, there is a way. You should use a for loop, to find the both models and check the string value they contain. Assuming they have the same parent (Example:)

1 Getting the child table

For deciding whatever is the model to analyze, we need a table that contains the model's object. We can do this by calling :GetChildren() over the models parent (On this case, Workspace):

local table = game.Workspace:GetChildren()

2 Determining the models.

Once we have multiple childs with the same Name, :FindFirstChild()and :WaitForChild() would lead the script to the malfunction. And calling :GetChildren() also returns data that we don't need. That's why we need to divide the wheat from the weed:

local table = game.Workspace:GetChildren()
for i,v in pairs (table) do
    if v.Name == "Model" then
        print("Model Detected")
    end
end

3 Determining the model with the right string value.

Both models have the string contained, called Value, and now it's just a question to compare the values. After you get the right model, you may highlight it. On this case, I will change the model's name.

local table = game.Workspace:GetChildren()
for i,v in pairs (table) do
    if v.Name == "Model" then
        print("Model Detected")
        if v:FindFirstChild("Value").Value == "Correct" then
            print("Right Model Detected")
            v.Name = "RIGHT MODEL HERE"
        end
    end
end

4 Connect it to the button.

Once you have a button, you may connect this script under the button, once no Parent depencies are existent. Don't forget to use the MouseButton1Click() event:

--Copy the source to a script under the button.
script.Parent.MouseButton1Click:connect(function()
    local table = game.Workspace:GetChildren()
    for i,v in pairs (table) do
        if v.Name == "Model" then
            print("Model Detected")
            if v:FindFirstChild("Value").Value == "Correct" then
                print("Right Model Detected")
                v.Name = "RIGHT MODEL HERE"
            end
        end
    end
end)

Hope I helped you!

Ad
Log in to vote
0
Answered by 8 years ago

From information I gathered, you will have several of these models and only one will have the value containing "correct".

First thing you should do is put all those models in serperate model, so you could just use GetChildren on them. Otherwise you will have to manually put them inside a table.

local container = modelWhichContainsTheMentionedModels
local children = container:GetChildren()

local correct
-- Iterate through all of the models
for num, model in ipairs( children ) do
    local value = model:FindFirstChild( "NameOfValue" )

    -- Just in case check if the value exists, and if it's the 'correct' one
    if value and value.Value == "correct" then
        -- Now store the correct model and break out of the loop
        correct = model
        break
    end
end

if correct then
    -- Do something with the 'correct' model
else
    -- Something went wrong, and none of the models were correct
end

Answer this question