• Tables not working as I'd like them to.
    3 replies, posted
I've been stuck on my latest project (A car racing gamemode) .. I have a networked string with the players cars in that comes off the MySQL Database. Each car is assigned an ID (A,B,C,D etc) and the players cars are stored like this. A,B,C,D,E. I then explode the string using [CODE]local car_table = string.Explode(",", ply:GetNWString("cars"))[/CODE] I also print the table for debugging reasons. Problem: I've created a script to check if a player owns a car: [CODE]function OwnCar(ply) local cars = ply:GetNWString("cars") if cars then car_table = string.Explode(",", cars) if car_table["A"] then print("True") else print("False") end end end concommand.Add("checkcar", OwnCar) [/CODE] But it always fucking returns false , no matter what I do. I know that "A" is in the table because I printed it earlier. I'm stuck on this one, Any ideas?
Anything between [ and ] in a table is the key, not the value. You're checking the key "A" of table car_table, which doesn't exist. You want to check for the value, so you'd do: [lua]if ( table.HasValue( car_table, "A" ) ) then print( "True" ) else print( "False" ) end[/lua]
[QUOTE=Nerdeboy;35012062]Anything between [ and ] in a table is the key, not the value. You're checking the key "A" of table car_table, which doesn't exist. You want to check for the value, so you'd do: [lua]if ( table.HasValue( car_table, "A" ) ) then print( "True" ) else print( "False" ) end[/lua][/QUOTE] Oh my god, thank you so much. Also another question.. Would this table method be better than using string.find(cars,"A")?
I haven't worked much with string.find, but if that returns the same thing, it would be faster than creating a table and populating it with the string's contents. Unless you're using really, really big strings, it shouldn't make much of a difference though.
Sorry, you need to Log In to post a reply to this thread.