Jump to content

Bug Problem


justn

Recommended Posts

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
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
  • Moderators

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

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

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
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

You(all) have to use string.match not string.find, to make this code working correctly.

Last time I am saying it.

string.find

Looks 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.match

Looks 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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...