• table.ToString() help
    4 replies, posted
So ive just tried to add a ingame friend system, where i type sphere_add <name> i then check if the name is valid and or is on the server, if it is i add it a table: SP.Friends i then added another concommand where it prints out everyone in the table with this code: [lua] function checkFriends() local friends = table.ToString(SP.Friends, "Friends: ", true) print(SP.Friends) end concommand.Add("sphere_friends", checkFriends) [/lua] But heres the weird part, it prints out a different table than i chose, instead it prints out my "names" table [IMG]http://puu.sh/ccBOR/e004094beb.jpg[/IMG] [editline]15th October 2014[/editline] wow im stupid............... [editline]15th October 2014[/editline] Sorry about that, i thought i was stupid, but i gave you guys old code. [lua] function checkFriend() local friends = table.ToString(SP.Friends, "Friends: ", true) print(friends) end concommand.Add("sphere_friends", checkFriend) [/lua] Still getting the names table instead of my friends table :suicide:
[QUOTE=Invule;46235488]So ive just tried to add a ingame friend system, where i type sphere_add <name> i then check if the name is valid and or is on the server, if it is i add it a table: SP.Friends i then added another concommand where it prints out everyone in the table with this code: [lua] function checkFriends() local friends = table.ToString(SP.Friends, "Friends: ", true) print(SP.Friends) end concommand.Add("sphere_friends", checkFriends) [/lua] But heres the weird part, it prints out a different table than i chose, instead it prints out my "names" table [IMG]http://puu.sh/ccBOR/e004094beb.jpg[/IMG] [editline]15th October 2014[/editline] wow im stupid............... [editline]15th October 2014[/editline] Sorry about that, i thought i was stupid, but i gave you guys old code. [lua] function checkFriend() local friends = table.ToString(SP.Friends, "Friends: ", true) print(friends) end concommand.Add("sphere_friends", checkFriend) [/lua] Still getting the names table instead of my friends table :suicide:[/QUOTE] Can i see how you use the SP.Friends table? Here's an example how table.ToString works: [url]https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexfd89-2.html[/url] Also you want to make your functions local, like this [lua] local function checkFriend() local friends = table.ToString(SP.Friends, "Friends: ", true) print(friends) end concommand.Add("sphere_friends", checkFriend) [/lua]
[lua] local SP = {} SP.Friends = {} function checkFutureFriend(name) local names = {} for k,v in pairs(player.GetAll() ) do if ( v:GetName():lower():match(name:lower()) ) then table.insert(names, v) end end // If 0 players were found then if (table.Count(names) == 0 ) then print("hi") return false end //If more than one player were found then if (table.Count(names) > 1) then print("ho") return false end return names[1] end //Adding friend function AddFriend(ply, cmd, args) local futurefriend = checkFutureFriend(args[1]) //Check the name for their future friend local friends = table.ToString(SP.Friends, "Friends: ", true) if IsValid(futurefriend) then table.insert(SP.Friends, names) //Grab the future friend off of our amazing names table then add it to our sp.friends table print("added friend" .. args[1]) end end concommand.Add("sphere_add", AddFriend) //Printing out every friend in the sp.friends table function checkFriend() local friends = table.ToString(SP.Friends, "Friends: ", true) print(friends) end concommand.Add("sphere_friends", checkFriend) [/lua] Sorry im not the best in lua, im still coming along, sorry if you notice some bad coding or other errors on the way.
[QUOTE=Invule;46235738][lua] local SP = {} SP.Friends = {} function checkFutureFriend(name) local names = {} for k,v in pairs(player.GetAll() ) do if ( v:GetName():lower():match(name:lower()) ) then table.insert(names, v) end end // If 0 players were found then if (table.Count(names) == 0 ) then print("hi") return false end //If more than one player were found then if (table.Count(names) > 1) then print("ho") return false end return names[1] end //Adding friend function AddFriend(ply, cmd, args) local futurefriend = checkFutureFriend(args[1]) //Check the name for their future friend local friends = table.ToString(SP.Friends, "Friends: ", true) if IsValid(futurefriend) then table.insert(SP.Friends, names) //Grab the future friend off of our amazing names table then add it to our sp.friends table print("added friend" .. args[1]) end end concommand.Add("sphere_add", AddFriend) //Printing out every friend in the sp.friends table function checkFriend() local friends = table.ToString(SP.Friends, "Friends: ", true) print(friends) end concommand.Add("sphere_friends", checkFriend) [/lua] Sorry im not the best in lua, im still coming along, sorry if you notice some bad coding or other errors on the way.[/QUOTE] Hey, you add friends to the table inccorectly. Instead of [lua] table.insert(SP.Friends, names) //Grab the future friend off of our amazing names table then add it to our sp.friends table[/lua] try [lua] table.insert(SP.Friends, futurefriend ) //Grab the future friend off of our amazing names table then add it to our sp.friends table [/lua] in your AddFriend function. EDIT: Also instead of [lua] for k,v in pairs(player.GetAll() ) do if ( v:GetName():lower():match(name:lower()) ) then table.insert(names, v) end end[/lua] try [lua] for k,v in pairs(player.GetAll() ) do if ( v:GetName():lower():match(name:lower()) ) then table.insert(names, v:GetName()) end end [/lua] since you add entities to the names table instead of names.
[IMG]http://puu.sh/ccGko/444fe25cc1.jpg[/IMG] Works like a charm, thanks Amic :)
Sorry, you need to Log In to post a reply to this thread.