Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is string.gmatch is returning nil's?

I'm trying to test the string.gmatch iterator. Problem is, the captures I'm specifying are somehow transforming to nil!

local Colors = {}
print(script.Colors.Value)
for k, v in string.gmatch(script.Colors.Value, "([%a]+)") do	
	print(v)
end

The child of the script is the stringvalue Colors, which holds the string, "Hello World, Die People,Apple" Yet my output is

nil >nil >nil >nil >nil >nil

1 answer

✓ Accepted answer

string.gmatch does not work quite like pairs. It only returns matches, nothing else.

--Added the next two lines just to be safe.
script:WaitForChild("Colors")
repeat wait() until script.Colors.Value ~= ""

print(script.Colors.Value)
for match in string.gmatch(script.Colors.Value, "%a+") do --Not 'k', not 'v', just each match.
    print(match)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.