Why is foundPlayer returning nil?

I was making a script that lets you use the command !role, you can either choose me or any player to role and it will change their nametag. The nametag part is fine, but me having no brain, I tried to add a for loop that will find a player based off a partial name, but foundPlayer returns nil.


				for i,v in ipairs(game.Players:GetPlayers()) do
					if string.match("me", string.lower(wordSplit[2])) then
						print(player)
						foundPlayer = player
					elseif string.match(string.lower(v.Name), string.lower(wordSplit[2])) then
						print(v)
						foundPlayer = v
					end
				end
				print(foundPlayer)

				local tag = foundPlayer.Character.Head.Nametag

wordSplit is a splitted string, and printing foundPlayer prints nil, and it errors that character is nil. Thanks for reading!

Hi!

It seems like you are trying to print the instance of foundPlayer. Since foundPlayer = v, you should try
print(foundPlayer.Name)

1 Like

P.S. Also, if I didn’t answer your question there, maybe you should try string.sub() and just start the sub string after !role.
!role
has 6 characters (including whitespace). So just do:

local substring = string.sub(wordSplit[2], 7)
if substring == "me" then
    print(player.Name)
    foundPlayer = v
elseif ...

Hey sorry I had to do something, but even if I do print player.name, foundPlayer is still nil or messed up since doing foundPlayer.Character is broken.

Okay I’ll try that, give me a second

Okay so I tried it, and I tried printing inside the loop, but it never printed. I’m pretty sure the loop isnt running, but I dont see why. Unless game.Players:GetPlayers() isnt returning a table I dont see a reason why it wouldnt run.

I hardly understand the script itself and this may be a REALLY dumb question but:

Where did the “player” came from? Is there a variable outside of this loop which refers to the game.Players.LocalPlayer?

Dropping the question, string.match needs the argument in order:

  • string s, representing the string it will search in to find the pattern. (Must be string, e.g. the player’s name)
  • string pattern, the pattern it will copies to find the matching one in string s. (Must be string, if it’s a number please do “1”, etc.)
  • number init, where it will start searching. E.g. if it’s given 6, it will start searching from the sixth letter in the string, skipping the letters before it. If not given, it’s set to 1 by default. (Must be number)

If you use the string.match in an if, and those 3 arguments are true, the script written inside it will be executed.

I don’t know if that’s the case because I never studied things like this before. I’m just taking a wild guess.

player is gotten from a PlayerAdded event, Ill try to rearrange the arguments to see if I can get it working. Also, I’m trying to make it so you can type for example, “meme”, and it will find the player “MemezyDev”

Okay so I am pretty sure they’re in the right order, I’ll resend the lines but same issue. Either I’m bad with strings or I’m just an idiot, foundPlayer isn’t having its value changed, because setting foundPlayer to the player works perfectly.

	local foundPlayer

				for i,v in ipairs(game.Players:GetPlayers()) do
					print(v.Name)
					if string.lower(tostring(wordSplit[2])) ==  "me" then
						foundPlayer = player
						print(foundPlayer.Name)
					elseif string.match(string.lower(v.Name), string.lower(tostring(wordSplit[2]))) then
						print(v.Name)
						foundPlayer = v
					end
				end
				
				local tag = foundPlayer.Character.Head.Nametag

1 Like

Wow, I’m an idiot, I didn’t realize I removed the first index inside of wordSplit, so I was checking the second index instead of the first. I changed them around and now it works perfectly. Thanks to everyone who tried to help!

1 Like