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

How do I find the first empty value?

Asked by 5 years ago

If I have StringValues that are each named a number, 1,2,3,4,5 (and so on) How can I create a script using :GetChildren() which gets the first StringValue that has no .Value and adds a value to it?

Like if "1" has a value, but "2" doesn't, "2" would get the value "Ketchup" (All the numbers after 2 would still have no .Value)

1 answer

Log in to vote
1
Answered by
oreoollie 649 Moderation Voter
5 years ago

This can be done using a General For Loop to iterate through all of the Children of an object and compare their values to an empty string, then breaking the loop. Here's some code to do that.

function writeToFirstEmptyStringValue(obj, newValue)
    for _, v in ipairs(obj:GetChildren) do
        if v.IsA("StringValue") then
            if v.Value == "" then
                v.Value = newValue
                break
            end 
        end
    end
end

writeToFirstEmptyStringValue(workspace.StringValues, "Ketchup")

If my answer solved your problem please don't forget to accept it. Thanks!

0
not general, generic. your code also errors and function name is disgusting. thank you. Elixcore 1337 — 5y
Ad

Answer this question