string.find Question

Hi, I’m just making a script that should kick players that use a gametag without beeing admin on my server for example.

My script is like this:

[lua]
if !v:IsAdmin() and string.find(v:SteamName(), string.lower("[GT-Admin]")) or string.find(v:SteamName(), string.lower("[GT-A]")) then
game.ConsoleCommand(“ulx kick “”…v:Name()…”" “Using Admin tag. You are not an Admin.”
")
end
[/lua]

The problem is, that string.find returns like 7 or 2 and similar.
What I simply want if: Is this sentence “[GT-Admin]” is found, ANYWHERE in the name of the player. Then kick him.
Not where its found, or partly found and all that string.find does to me.

Thanks for helping!

If it returns anything other than nil, the sub string is found. I don’t see what the problem is.

[lua]
if 7 then – this condition is true

end
[/lua]

You have to lower the name of the player too.

[lua]
string.find( string.lower(v:SteamName()), string.lower("[GT-Admin]"))

[/lua]

You’re totally right here. I really had to facepalm my self there. I thought first that it it might like output value 2, if its not found too or partly. Anyways. Thanks for clearing it up!

Yeah, good point. Just did so.
I got a new problem now though.

[lua]
for k,v in pairs(player.GetAll()) do
if !v:IsAdmin() and !v:IsSuperAdmin() and string.find(string.lower(v:SteamName()), string.lower("[HB-Admin]")) != nil or string.find(string.lower(v:SteamName()), string.lower("[HB-A]")) != nil then
game.ConsoleCommand(“ulx kick “”…v:Name()…”" “Using Admin tag. You are not an Admin.”
“)
elseif !v:IsSuperAdmin() and string.find(string.lower(v:SteamName()), string.lower(”[HB-SuperAdmin]")) != nil or string.find(string.lower(v:SteamName()), string.lower("[HB-SA]")) != nil then
game.ConsoleCommand(“ulx kick “”…v:Name()…”" “Using SuperAdmin tag. You are not an SuperAdmin.”
“)
elseif !v:IsSuperAdmin() and string.find(string.lower(v:SteamName()), string.lower(”[HB-Owner]")) != nil or string.find(string.lower(v:SteamName()), string.lower("[HB-CoOwner]")) != nil or string.match(string.lower(v:SteamName()), string.lower("[HB-CO]")) != nil then
game.ConsoleCommand(“ulx kick “”…v:Name()…”" “Using Owner tag. You are not the Owner.”
")
end
end
[/lua]

It kicks me out and says “You’re not admin”. That’s odd.
Becase I set, if that player is not Admin or Superadmin and has one of those tags, THEN kick.

I’ve double checked, I’m SuperAdmin and it returns true if I do : lua_run_cl print(LocalPlayer():IsSuperAdmin())

So this is odd… Or am I missing something really obvious again?

You need to use parenthesis, currently you’ll always kick if the player has the HB-A tag

[editline]9th January 2012[/editline]

Also it’s unnecessary to use string.lower on static strings, just use [hb-a] as a string directly.

Thanks! Works now :slight_smile:

Just a correction. Input true as the fourth parameter. string.find uses patterns, so [GT-A] would match either character G, any number of character T or a character A. So it would trigger just by having a G, T or an A in your name. The fourth parameter turns on the plain mode if it’s true, then it doesn’t use patterns.

That helped indeed.
Everything works as it should. Except it kick players with ONLY [HB] tag, no -Admin or -Superadmin extension. And it says those [HB] players aren’t Owner.

[lua]
for k,v in pairs(player.GetAll()) do
if (string.find(string.lower(v:SteamName()), “[hb-admin]”, 1, true) or string.find(string.lower(v:SteamName()), “[hb-a]”, 1, true)) and (!v:IsAdmin() and !v:IsSuperAdmin()) then
game.ConsoleCommand(“ulx kick “”…v:Name()…”" “Incorrect HeartBit tag. You are not an Admin.”
“)
elseif (string.find(string.lower(v:SteamName()), “[hb-superadmin]”, 1, true) or string.find(string.lower(v:SteamName()), “[hb-sa]”, 1, true)) and (!v:IsSuperAdmin()) then
game.ConsoleCommand(“ulx kick “”…v:Name()…”” “Incorrect HeartBit tag. You are not an SuperAdmin.”
“)
elseif (string.find(string.lower(v:SteamName()), “[hb-owner]”, 1, true) or string.find(string.lower(v:SteamName()), “[hb-coowner]”, 1, true) or string.match(string.lower(v:SteamName()), “[hb-co]”, 1, true)) and (!v:IsSuperAdmin()) then
game.ConsoleCommand(“ulx kick “”…v:Name()…”” “Incorrect HeartBit tag. You are not the Owner.”
")
end
end
[/lua]

String.match does not have plaintext support, why are you even using that function in the first place when you’re using string.find everywhere else?. Otherwise I can’t see anything wrong with the code

Oh crap. I forgot to replace that one with string.find.
Damn I better get sleep, I feel I forgot all Lua today >.>

Thanks for helping ^^