• Finding by part of name?
    10 replies, posted
Hey there, experimenting with the whole Lua thing again, is there a way to select a player from the server, if he has a string in his name for example "crazy" as the string? Edit: Also, I've just been looking around the wiki, how would I implement this, use a function or what? [LUA]if attacker:IsPlayer() then return false end[/LUA] Could I just have it as this: [LUA] function attacked(ply) if attacker:IsPlayer() then return false end [/LUA]
For the first question: [lua] function FindPlayerByPartOfName( part ) for _, v in pairs(player.GetAll()) do if( string.find(string.lower(v:Nick()), string.lower(tostring(part)) then return v; end; end; end; function KillAllCrazyPeople(ply, cmd, args) if (!ply:IsAdmin()) then return end; for _, v in pairs(FindPlayerByPartOfName("crazy") do v:Kill(); end; end; concommand.Add("kill_crazypeople", KillAllCrazyPeople); [/lua] For the second question: [lua] function GM:EntityTakeDamage( ent, inflictor, attacker, amount ) if ( !ent:IsPlayer() ) then return end; if ( attacker:IsPlayer()) then return end; hook.Call("PlayerTakeDamage", self, ent, inflictor, attacker, amount); end; [/lua]
Excellent man, these can just go into the server autorun, or within a gamemode itself?
In the gamemode. (If you want to put this in autorun you'll need to make hooks)
[LUA] function GM:PlayerSpawn( ply ) if player:Name() == "^2OctaOctaGon" then player:ChatPrint("OctaOctagon the Admin just Respawned!") else player:ChatPrint("A non-admin has just repspawned...") end end hook.Add("PlayerSpawn", "Plyspawn", GM:PlayerSpawn)[/LUA] Just wrote this, any ideas why it doesn't work, I don't have it in a gamemode because it has a hook, does it need to be put in client autorun or the server autorun, or even the main autorun folder?
Don't put (GM:) when your code is hooked or not in a gamemode. Also, use ply:Nick()... I don't know what is ply:Name(), I never used it. [LUA]function PlayerSpawn( ply ) if ply:Nick() == "^2OctaOctaGon" then -- Not a good idea to use a name (steamId is better) for k, v in pairs(player.GetAll()) do -- player: doesn't mean all the player... v:ChatPrint("OctaOctagon the Admin just Respawned!"); end; else for k, v in pairs(player.GetAll()) do ply:ChatPrint("A non-admin has just repspawned..."); end; end; end ; hook.Add("PlayerSpawn", "Plyspawn", PlayerSpawn)[/lua] And you need to put this in the server folder (autorun/server/).
Excellent, thanks. I assume the (GM:) is obviously used for Gamemodes and nothing else right?
[QUOTE=sphinxa279;22597767]Excellent, thanks. I assume the (GM:) is obviously used for Gamemodes and nothing else right?[/QUOTE] Yes
So, does that man I can use functions such as the InitialPlayerSpawn as well as all the other gamemode functions as long as I hook them and don't include the (GM:)?
[QUOTE=sphinxa279;22598133]So, does that man I can use functions such as the InitialPlayerSpawn as well as all the other gamemode functions as long as I hook them and don't include the (GM:)?[/QUOTE] Yea pretty much.
Thankyou again, any hints or tips on using Lua within Garrysmod, or even general coding hints would be apppreciated.
Sorry, you need to Log In to post a reply to this thread.