Jump to content

PROBLEM with auto teams


Recommended Posts

  
function getTeamFromPartOfName ( teamName ) 
    if ( teamName and type ( teamName ) == "string" ) then 
        local teamsFound = { } 
        local teamFound = getTeamFromName ( teamName ) 
        if ( teamFound ) then 
            return teamFound 
        end 
  
        for _, team in ipairs ( getElementsByType ( "team" ) ) do 
            if ( getTeamName ( team ):lower ( ):find ( teamName:lower ( ), 1, true ) ) then 
                table.insert ( teamsFound, team ) 
            end 
        end 
  
        if ( #teamsFound == 1 ) then 
            return teamsFound [ 1 ] 
        end 
    end 
  
    return false 
end 
  
function tag() 
    for i, player in ipairs(getElementsByType("player")) do 
        table.insert(teams,{ 
            t1 = getElementData(player,"text1"), 
            t2 = getElementData(player,"text2")}) 
    end 
end 
addCommandHandler("set1",tag) 
function test(source) 
    for i, player in ipairs(getElementsByType("player")) do 
        for k,v in ipairs(teams) do 
        local nnaem = getPlayerName(player) 
        local t1 = getElementData(player,"text1") 
        string.find(k,nnaem) 
        setPlayerTeam(player,getTeamFromPartOfName(k)) 
    end 
    end 
end 
addCommandHandler("set",test) 
  

this script should work like an auto team but when i join and try the commands nothing happens i don't get setted automatically

Link to comment
when a player joins or he was in the server already he gets automatically placed in a team if any part of his name matches the teamname

Try this out:

  
function getTeamFromPartOfName ( teamName ) 
    if ( teamName and type ( teamName ) == "string" ) then 
        local teamsFound = { } 
        local teamFound = getTeamFromName ( teamName ) 
        if ( teamFound ) then 
            return teamFound 
        end 
  
        for _, team in ipairs ( getElementsByType ( "team" ) ) do 
            if ( getTeamName ( team ):lower ( ):find ( teamName:lower ( ), 1, true ) ) then 
                table.insert ( teamsFound, team ) 
            end 
        end 
  
        if ( #teamsFound == 1 ) then 
            return teamsFound [ 1 ] 
        end 
    end 
  
    return false 
end 
  
addEventHandler("onResourceStart", resourceRoot, 
    function () 
        for _, player in ipairs ( getElementsByType("player") ) do 
            local team = getTeamFromPartOfName(getPlayerName(player)); 
            if ( isElement(team) ) then 
                setPlayerTeam(player, team); 
            end 
        end 
    end 
) 
  
addEventHandler("onPlayerJoin", root, 
    function () 
        if ( isElement(getTeamFromPartOfName(getPlayerName(source))) ) then 
            setPlayerTeam(source, getTeamFromPartOfName(getPlayerName(source))); 
        end 
    end 
) 

Link to comment

No but i think the problem is

local team = getTeamFromPartOfName(getPlayerName(player));

when the player joins he is not in a team

full server - side

  
local teams = {} 
function hex2rgb(hex) 
  hex = hex:gsub("#","") 
  return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) 
end 
function test(source) 
        local element = createElement("cw") 
        setElementData(element,"text1","EPIC") 
        setElementData(element,"text2","T2") 
        setElementData(element,"color1","#FF0000") 
        setElementData(element,"color2","#00FFFF") 
        setElementData(element,"score1",0) 
        setElementData(element,"score2",0) 
        local t1 = getElementData(element,"text1") 
        local c1 = getElementData(element,"color1") 
        local t2 = getElementData(element,"text2") 
        local c2 = getElementData(element,"color2") 
        local s1 = getElementData(element,"score1") 
        local r, g, b =  hex2rgb(c1) 
        local r1, g1, b1 =  hex2rgb(c2) 
        createTeam(t1, r, g, b) 
        createTeam(t2,r1,g1,b1) 
    end 
addEventHandler("onResourceStart", resourceRoot, test) 
function ap( source, commandName, idnum) 
    for i, player in ipairs(getElementsByType("cw")) do 
        if idnum == "1" then 
            setElementData(player,"score1",getElementData(player,"score1")+1) 
            local mast = getPlayerName(source) 
            local t1 = getElementData(player,"text1") 
            local teamid = getTeamFromPartOfName(t1) 
            local teamn = getTeamName(teamid) 
            outputChatBox("("..mast..") Added 1 Point to "..teamn) 
            elseif idnum == "2" then 
                setElementData(player,"score2",getElementData(player,"score2")+1) 
                local mast = getPlayerName(source) 
                local t2 = getElementData(player,"text2") 
                local teamid = getTeamFromPartOfName(t2) 
                local teamn = getTeamName(teamid) 
                outputChatBox("("..mast..") Added 1 Point to "..teamn) 
        end 
    end 
end 
addCommandHandler("ap",ap) 
function dp(thePlayer, commandName, idnum) 
        for i, player in ipairs(getElementsByType("cw")) do 
            if idnum == "1" then 
                setElementData(player,"score1",getElementData(player,"score1")-1) 
                outputChatBox( "Removed score "..getElementData(player,"score1").."") 
            elseif idnum == "2" then 
                setElementData(player,"score2",getElementData(player,"score2")-1) 
                outputChatBox( "Removed score "..getElementData(player,"score2").."") 
            end 
    end 
end 
addCommandHandler("dp",dp) 
function stc(source, commandName, idnum, color) 
    for i, player in ipairs(getElementsByType("cw")) do 
        if idnum == "1" then 
        local r, g, b =  hex2rgb(color) 
        local t11 = getElementData(player,"text1") 
        local t1 = getTeamFromPartOfName(t11) 
        setTeamColor(t1,r,g,b) 
        setElementData(player,"color1",color) 
        local uVehicle = getPedOccupiedVehicle( player ) 
        setVehicleColor(uVehicle,r,g,b) 
        local co = getElementData(player,"color1") 
        outputChatBox("changed color"..co) 
        elseif idnum == "2" then 
            local r, g, b =  hex2rgb(color) 
            local t11 = getElementData(player,"text2") 
            local t1 = getTeamFromPartOfName(t11) 
            setTeamColor(t1,r,g,b) 
            setVehicleColor(player,r,g,b) 
            setElementData(player,"color2",color) 
            local co = getElementData(player,"color2") 
            outputChatBox("changed color"..co) 
        end 
    end 
end 
addCommandHandler("stc",stc) 
  
function tsn(source, command, idnum,name,playerSource) 
    for i, player in ipairs(getElementsByType("cw")) do 
        if idnum == "1" then 
        local name1 = getElementData(player,"text1") 
        local teamname = getTeamFromPartOfName(name1) 
        setTeamName (teamname, name)   
        setElementData(player,"text1",name) 
        outputChatBox("setted name"..name1.."") 
        elseif idnum == "2" then  
        local name2 = getElementData(player,"text2") 
        local teamname = getTeamFromPartOfName(name2) 
        setTeamName (teamname, name)   
        setElementData(player,"text2",name) 
        outputChatBox("setted name"..name2.."") 
    end 
    end 
end 
addCommandHandler("tsn",tsn) 
  
  
function getTeamFromPartOfName ( teamName ) 
    if ( teamName and type ( teamName ) == "string" ) then 
        local teamsFound = { } 
        local teamFound = getTeamFromName ( teamName ) 
        if ( teamFound ) then 
            return teamFound 
        end 
  
        for _, team in ipairs ( getElementsByType ( "team" ) ) do 
            if ( getTeamName ( team ):lower ( ):find ( teamName:lower ( ), 1, true ) ) then 
                table.insert ( teamsFound, team ) 
            end 
        end 
  
        if ( #teamsFound == 1 ) then 
            return teamsFound [ 1 ] 
        end 
    end 
  
    return false 
end 
function findPlayer(namepart) 
    for i, player in ipairs(getElementsByType("player")) do 
        local name = getPlayerName(player) 
        if string.find(name:lower(), namepart:lower(), 1, true) then 
            return player, name 
        end 
    end 
    return false     
end 
  
addEventHandler("onResourceStart", resourceRoot, 
    function () 
        for _, player in ipairs ( getElementsByType("player") ) do 
            local pp = findPlayer(player) 
            local team = getTeamFromPartOfName(getPlayerName(pp)); 
            if ( isElement(team) ) then 
                setPlayerTeam(player, team); 
            end 
        end 
    end 
) 
  
addEventHandler("onPlayerJoin", root, 
    function () 
        if ( isElement(getTeamFromPartOfName(getPlayerName(source))) ) then 
            setPlayerTeam(source, getTeamFromPartOfName(getPlayerName(source))); 
        end 
    end 
) 
  

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