Simple LUA issues

I’m starting to make a custom administration mod for my server, because apparently all of them are broken and dysfunctional. (Exsto, Evolve, and ULX not loading commands in)
This is the code I decided to start with, as most of the other commands would revolve around findplayer(name) and updatePlayerList().


plist=player.GetAll()
function findplayer(name)
updatePlayerList()
table.foreach(plist,function(k,v) if v:GetName()==name then return v end end)
print("could not find player with given name "..name.." !")
end
function updatePlayerList()
	plist=player.GetAll()
end
function spawn(player,command,args)
	findplayer(args[1]):Spawn()
	print("attempted to spawn player ".. args[1])
end
concommand.Add("_spawn",spawn)

However, when I try to execute both
_spawn “Username”
and
_spawn Username
My code returns that it could not find the player specified.
The username matches exactly what it should have been, is there something wrong with my findplayer()?

Fixed!
I was using table.foreach(), which doesn’t appear to work.
using the for k,v in pairs(plist) worked perfectly.

you need to fix your find player function aswell as it doesnt return anything if it doesnt find the player, yet you are calling a userdata method on it.

if you enter in a name which doesnt match to a player you will get errors.

if you make it return false then check if its false when you call it, it should work for anything.