1. Post #1
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    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

    Code:
    function suggestionstart( ply, txt )
    	local names =  file.Read("names/names.txt")
    	local player = string.Explode("\n", 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.

  2. Post #2
    Gold Member
    Nevec's Avatar
    June 2008
    1,091 Posts
    You have an else break which will terminate the loop if the condition doesn't work.

  3. Post #3
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    Even when removed it didn't work - I added it there because I thought that would fix it.

  4. Post #4
    metromod.net
    ChewGum's Avatar
    October 2007
    1,092 Posts
    Print the table.

  5. Post #5
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    I did - the value of the table is DrFire and Kamakazi Melon. (two separate values)

  6. Post #6
    Gold Member
    Entoros's Avatar
    October 2008
    2,318 Posts
    hook.Add("OnPlayerChat","SuggestionStart",function(ply,text,toall)
    	for _,v in ipairs(string.Explode("\n",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)

    Your code is something like that?

  7. Post #7
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    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)

  8. Post #8
    Gold Member
    Entoros's Avatar
    October 2008
    2,318 Posts
    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.

  9. Post #9
    Gold Member
    bobthe2lol's Avatar
    July 2007
    1,370 Posts
    if ply:Nick() == v. V is a player, ply:Nick() is a string. ply:Nick() == v:Nick() should work

  10. Post #10
    Gold Member
    Nevec's Avatar
    June 2008
    1,091 Posts
    if ply:Nick() == v. V is a player, ply:Nick() is a string. ply:Nick() == v:Nick() should work
    No, v is a string.

  11. Post #11
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    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.

  12. Post #12
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    -snip-

  13. Post #13
    Gold Member
    The Best's Avatar
    August 2005
    1,085 Posts
    Update: I updated the script a little, look at these lines:

    Code:
    print(string.len(string.Trim(v)),string.len(ply:Nick()))
    	print(v,ply:Nick())
    From the whole script,
    Code:
    function suggestionstart( ply, txt )
    	local names =  file.Read("names/names.txt")
    	local playors = string.Explode("\n", 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 - :| 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:Ni ck()))

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