Tables, loop, and conditional statements

I have a problem: In this following excerpt, the function runs until it hits “if ply:Nick() == v then”.
I see no reason for it to fail.

Inside names.txt is:
DrFire
Kamakazimelon


function suggestionstart( ply, txt )
	local names =  file.Read("names/names.txt")
	local player = string.Explode("
", names)
	for k, v in pairs(player) do
		if ply:Nick() == v then
		sOn = 1
		timer.Create("sugOn", 2, 1, RunConsoleCommand, "say", "Suggestion Bot is now online! To make a suggestion, type !s <suggestionhere> to make your suggestion.")
		else break
		end
	end
end
hook.Add("OnPlayerChat","startsuggestion",suggestionstart)

Please help, thanks.

You have an else break which will terminate the loop if the condition doesn’t work.

Even when removed it didn’t work - I added it there because I thought that would fix it.

Print the table.

I did - the value of the table is DrFire and Kamakazi Melon. (two separate values)

[lua]hook.Add(“OnPlayerChat”,“SuggestionStart”,function(ply,text,toall)
for _,v in ipairs(string.Explode("
",file.Read(“names/names.txt”))) do
if ply:Nick() == v then
sOn = 1
timer.Simple(2,function() RunConsoleCommand(“say”,“Suggestion Bot is now online! To make a suggestion, type !s <suggestionhere> to make your suggestion.”) end)
end
end
end)[/lua]

Your code is something like that?

Yea, pretty much. I’m using names because this all has to be clientside - also, what’s the difference between pairs and ipairs (in the for k,v loops)

ipairs (or integer pairs) only works with tables that have integer keys; this is probably most tables you’ll be using.

For example, {“string”,10,{“table”},Vector(0,0,0)} has integer keys. The data types don’t matter; since you don’t give them keys it automatically gives them keys of 1, 2, 3, and 4, respectively.

A table that doesn’t have integer keys would be one where you assign a value a key that, well, isn’t an integer.

local table = {}
table[“Non-integer key”] = “value”

print(table[“Non-integer key”]) will print "value.

The reason you would use ipairs is because it runs faster than pairs, so use it whenever possible.

if ply:Nick() == v. V is a player, ply:Nick() is a string. ply:Nick() == v:Nick() should work

No, v is a string.

Okay, I’m seriously confused now. I have tried everything - this thing will not work. I’ve printed v, and printed ply:Nick(), both of them are DrFire, yet when it hits the if, then loop, it doesn’t work.

-snip-

Update: I updated the script a little, look at these lines:


print(string.len(string.Trim(v)),string.len(ply:Nick()))
	print(v,ply:Nick())

From the whole script,


function suggestionstart( ply, txt )
	local names =  file.Read("names/names.txt")
	local playors = string.Explode("
", names)  
	for k, v in ipairs(playors) do
	print(string.len(string.Trim(v)),string.len(ply:Nick()))
	print(v,ply:Nick())
		if string.lower(ply:Nick()) == string.lower(v) then
		print("It works!")
		sOn = 1
		timer.Create("sugOn", 2, 1, RunConsoleCommand, "say", "Suggestion Bot is now online! To make a suggestion, type !s <suggestionhere> to make your suggestion.")
		break
	end
	end
end
hook.Add("OnPlayerChat","startsuggestion",suggestionstart)

Now, here’s the thing. Once the function has been run, I get the following in my console.

9 6
DrFire DrFire
18 6
Kamikazemelon - :expressionless: DrFire

What the hell is this? It’s telling me there are 9 characters in my name. But wait, they could be spaces. I check the text file, no spaces, I even do this:

print(string.len(string.Trim(v)),string.len(ply:Nick()))

As you can see, trimming it does nothing.
I’m confuzzled. Help?