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

bad argument #2 to 'match' (number expected, got string)?

Asked by 5 years ago

I need help with this, it keeps saying as an error http://prntscr.com/lfpm1n

0
How do you code with that font? It's like the cousin of comic sans. EzraNehemiah_TF2 3552 — 5y
0
Also post the errors on the question rather than in a prntscr. User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Methods

Methods are functions that belong to a specific object or table. When you call it, you use : before the function's name. When you call a method, the first argument is automatically set. That argument is called, self. For example, Destroy() is a commonly used method. Clone() as well. So many different types of instances have Destroy() or Clone() built in. When you call it, how does the script know which part to destroy since you aren't setting any arguments? Remember self.

workspace.Part:Destroy()
--This knows to destroy the part because the method can automatically obtain "self"

workspace.Part.Destroy(workspace.Part)
--When we call it like a normal function, we don't automatically get "self", so we need to define what we want to destroy ourselves.

Both the lines of code do the exact same thing.


string.match

String.match contains 3 arguments. The first one is mandatory, the string you are looking for a match inside of. The second mandatory one is the string you want to find in the original one. The third, but optional, argument is an integer (or number) which tells the function what point they should start searching from. The third argument must be a number always.

local string = "My favorite color is blue or pink"
print(string.match(string, "o", 28))

nil


The Problem

Here is your code:

local Tplayer = game.Players:FindFirstChild(msg:match(v.Name, msg:sub(6)))

You are calling string.match as a method. Calling it as a method is perfectly fine! In this case, however, not so much. What you are actually doing is trying to find v.Name inside of msg starting from the msg:sub(6)th letter... The third argument must always be a number. It makes no sense to the script, so it throws an error. The simple solution is to not use it like a method in this case.

local Tplayer = game.Players:FindFirstChild(string.match(v.Name, msg:sub(6)))


Hope it helps!

Ad

Answer this question