justn Posted July 22, 2014 Share Posted July 22, 2014 Hi, so I have this script so when the player changes his name and if his new name contains '-|SxG|-' then his team will be set. But the thing is, most of the time when the player changes his name and it doesnt contain -|SxG|- he still gets set to the team. Code: function setTeam2(old,new) if old then if new then local name = getPlayerFromPartialName("-|SxG|-") if name then if team then setPlayerTeam(source,getTeamFromName("-|SxG|- Skilled Xtreme Gamers")) end else setPlayerTeam(source,nil) end end end end addEventHandler("onPlayerChangeNick",root,setTeam2) Link to comment
12p Posted July 22, 2014 Share Posted July 22, 2014 function setTeam2(old,new) if old and new then if string.find(getPlayerName(source), "-|SxG|-") then --built-in string function if team then setPlayerTeam(source, getTeamFromName("-|SxG|- Skilled Xtreme Gamers")) end else setPlayerTeam(source,nil) end end end addEventHandler("onPlayerChangeNick",root,setTeam2) I'm guessing it doesn't give troubles at all with that undeclared "team" variable, right? Link to comment
justn Posted July 22, 2014 Author Share Posted July 22, 2014 Bug.. If my name contains -|SxG|- and i change it to something else like "TEST" then i get set to the team. Link to comment
Et-win Posted July 22, 2014 Share Posted July 22, 2014 Not sure, but try checking with 'new'? Link to comment
12p Posted July 22, 2014 Share Posted July 22, 2014 Go and check the documentation for the onPlayerChangeNick event. Also, apply common sense and try to understand what your code does, how and why. I once did a step-by-step instruction, but now I don't feel like it. It's actually quite easy. Link to comment
Moderators IIYAMA Posted July 22, 2014 Moderators Share Posted July 22, 2014 use string.match in stead of string.find if string.match(getPlayerName(source), "-|SxG|-") then String find will return things when the letters do match, which isn't want you want. You want to match words. Link to comment
justn Posted July 23, 2014 Author Share Posted July 23, 2014 I tried this code below, it works. but, when the player puts -|SxG|- on his name, he doesn't get set to the team, but when he puts |PaS| he does.. can u help me with this problem ? local Table = { {"-|SxG|-","-|SxG|- Skilled Xtreme Gamers",255,155,0}, {"|PaS|","|PaS| Pro Adventure Skills",23,56,89}, } function setTeam2(old,new) for i,v in ipairs(Table) do if new then if string.find(new, v[1]) then --built-in string function if team then setPlayerTeam(source, getTeamFromName(v[2])) end else setPlayerTeam(source,nil) end end end end addEventHandler("onPlayerChangeNick",root,setTeam2) Link to comment
12p Posted July 23, 2014 Share Posted July 23, 2014 The "for" statement loops until you tell it to stop. And just by executing setPlayerTeam you're not doing it. Check what I added in your code, it may come handy for related coding: local Table = { {"-|SxG|-","-|SxG|- Skilled Xtreme Gamers",255,155,0}, {"|PaS|","|PaS| Pro Adventure Skills",23,56,89}, } function setTeam2(old,new) for i,v in ipairs(Table) do if new then if string.find(new, v[1]) then --built-in string function if team then setPlayerTeam(source, getTeamFromName(v[2])) break --Stop the "for" loop end else setPlayerTeam(source,nil) break --Stop the "for" loop end end end end addEventHandler("onPlayerChangeNick",root,setTeam2) Link to comment
justn Posted July 23, 2014 Author Share Posted July 23, 2014 Now, it only works for -|SxG|- team.. i want it to work for all the teams in the table Link to comment
Et-win Posted July 23, 2014 Share Posted July 23, 2014 else setPlayerTeam(source,nil) break --Stop the "for" loop end The problem. You are stopping the table by already the FIRST rule of the table if the place has not the tag from the team. local Table = { {"-|SxG|-","-|SxG|- Skilled Xtreme Gamers",255,155,0}, {"|PaS|","|PaS| Pro Adventure Skills",23,56,89}, } function setTeam2(old,new) local teamFound = false for i,v in ipairs(Table) do if new then if string.find(new, v[1]) then --built-in string function if team then setPlayerTeam(source, getTeamFromName(v[2])) teamFound = true break --Stop the "for" loop end end end end if (teamFound == false) then setPlayerTeam(source, nil) end end addEventHandler("onPlayerChangeNick",root,setTeam2) Link to comment
Moderators IIYAMA Posted July 23, 2014 Moderators Share Posted July 23, 2014 You(all) have to use string.match not string.find, to make this code working correctly. Last time I am saying it. string.findLooks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as well. If the pattern has captures, then in a successful match the captured values are also returned, after the two indices. string.matchLooks for the first match of pattern in the string s. If it finds one, then match returns the captures from the pattern; otherwise it returns nil. If pattern specifies no captures, then the whole match is returned. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. Link to comment
12p Posted July 23, 2014 Share Posted July 23, 2014 Actually, there's no difference in both except what they return. if string.find(new, v[1]) ~= nil then Link to comment
justn Posted July 23, 2014 Author Share Posted July 23, 2014 It works ! Ty, but 1 last thing. How do I check if a team has no players, then the team won't show in scoreboard, and let's say, if a player joins the server, and he gets set to the team. then the teams will show in scoreboard.. ? Link to comment
12p Posted July 23, 2014 Share Posted July 23, 2014 When someone leaves the team, if there's no players in that team element, destroy it. When someone tries to join an unexisting team, create that team element and THEN make the player join. That should be it. Some "if"s here and there, attached to onPlayerQuit and this code. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now