I have been trying to find a way to separate text from a string to search a part in a model, if the text has “Red Blue” it separates into “Red” and “Blue” and searches the parts with the same name and clones them infront of the player.
Is there a way to do that?
I have tried asking some friends and searching here but nothing appeared.
The string library provides string.gmatch
which can iterate through a string and gives you a match.
In order to use it you need to know about String Patterns.
The first parameter is the string we want to search in, the second is the pattern we want to look for.
In this case the pattern will be "%w+"
The %w
matches a single letter or number. The +
combines as many %w
(letter/number) as it can into one match.
So your code will end up looking a bit like this:
local stringToSearch = "mopy Dr Red Blue"
local modelToSearch = workspace
for match in string.gmatch(stringToSearch,"%w+") do
local thing = modelToSearch:FindFirstChild(match)
if not thing then
warn(match,"was not found in",modelToSearch:GetFullName())
end
thing = thing:Clone()
--set position
--parent to somewhere in workspace
end
1 Like
If all of the parts are explicitly named Red
, Blue
, or whatever the player decides to input, then you don’t need substring matching.
local text = "Red Blue"
local model = ...
local names = text:split()
for i, descendant in pairs(model:GetDescendants()) do
if table.find(names, descendant.Name) then
local clone = Part:Clone()
--
end
end
1 Like
Thanks, it perfectly worked as intended
Thanks for helping, you’re cool