Invalid argument #1 to 'match' (string expected, got table)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    An NPC-only leaderboard for rounds survived.
  2. What is the issue? Include screenshots / videos if possible!
    It does not work, instead giving me an error.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Different arguments, yes.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local names={"builder","man","guy","brick"}
local npc=string.match(game.Workspace:GetChildren(),names[math.random(1,#names)],1)
script.Parent.Text=game.Workspace:WaitForChild(npc).Name

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

:GetChildren returns a table, and string.match requires a string for the first argument. what are you trying to do? do you want to get a child of workspace which has “builder”, “man”, “guy”, or “brick” in its name?

1 Like

Yeah, im not going to explain the game’s enviroment as it is too chaotic and random to explain, so ill give you the RBXL.
bots.rbxl (152.5 KB)

1 Like

Basically, bots spawn in a random pattern inside of a box and they have a “ROUNDS SURVIVED” counter above their heads, they place randomly every 1-5 seconds blocks with random colors, shapes and sizes. Rocks spawn every half-minute, if they touch the rocks, they are killed and their counter is reset, if a block touches the rocks, its flinged and unanchored.

1 Like
local names={"builder","man","guy","brick"}
local npc
local randomName = names[math.random(1,#names)]
for _,child in workspace:GetChildren() do
	if string.match(child.Name,randomName,1) then
		npc = child
	end
end
script.Parent.Text=npc.Name
1 Like

Do you mean

workspace:FindFirstChild(names[math.random(#names)])

?

1 Like

Yes! Sorry for not seeing that using 1 at the start is near-useless… However, that doesnt work.

1 Like

That didnt work, it just got rid of the error and added the usual “Infinite yield possible on “””.

1 Like

look at edit (charcharcharcharchar)

1 Like

Unless your NPCs aren’t exactly named as what’s available in the names array, you can simplify your code to the above

1 Like

The table is also used to make randomly-generated names for them, so people can distinguish them in the Leaderboard.

1 Like

Then you can use the code I gave you

1 Like

Example: builder guy man brick, 5 rounds survived

1 Like

does this work?

local names={"builder","man","guy","brick"}

local npc = workspace:WaitForChild(names[math.random(#names)])

script.Parent.Text = npc.Name
1 Like

Is that a single username? “builder guy man brick”?

1 Like

Yes, however, it is just an example. They can also have multiple words, like “builder guy guy man”.

1 Like

At this point, just tag the NPCs or parent them to a folder. It’s more efficient than brute-forcing all children of the workspace for a particular phrase

1 Like

Thank you. However, now theres one more step;
I need to get all 24 Builders in there, without any duplicates. There is also the problem of getting the number of rounds they survived.

1 Like

Use a permuting algorithm to generate an NPC per permutation of the available words. This will naturally prevent you from generating an NPC with an existing name. Use a generator approach if you need to create NPCs at different times, but know that the generator will eventually exhaust if the NPCs with assigned names can be deleted (consider object-pooling). You can re-create the generator to start the process over.

The number of rounds a particular NPC survives should not be associated with their name, but the instance itself

They cannot be replaced when they die, they respawn, so they keep their names.