Jump to content

[help] Autoteams


Recommended Posts

Posted (edited)

the color of the team should get on the radar with the color of their team on blip

help me please :(:?:|

  
------------ 
-- Events -- 
------------ 
  
function playerJoined() 
    check(source) 
end 
addEventHandler("onPlayerJoin",getRootElement(),playerJoined) 
  
function playerChangedNick(oldNick,newNick) 
    -- Use timer to wait until the nick really has changed 
    setTimer(check,1000,1,source) 
end 
addEventHandler("onPlayerChangeNick",getRootElement(),playerChangedNick) 
  
function playerQuit() 
    removePlayerFromTeam(source) 
end 
addEventHandler("onPlayerQuit",getRootElement(),playerQuit) 
  
-- Check for ACL Groups on login/logout 
function loggedIn() 
    check(source) 
end 
addEventHandler("onPlayerLogin",getRootElement(),loggedIn) 
  
function loggedOut() 
    check(source) 
end 
addEventHandler("onPlayerLogout",getRootElement(),loggedOut) 
  
  
-- Holds the teams as defined in the settings 
local teams = {} 
  
--- 
-- Reads the settings and creates the teams if enabled. 
-- 
function initiate() 
    local rootNode = getResourceConfig("config.xml") 
    local children = xmlNodeGetChildren(rootNode) 
    if children == false then 
        return 
    end 
    for _,node in pairs(children) do 
        local attributes = xmlNodeGetAttributes(node) 
        local name = attributes.name 
        local color = {getColorFromString(attributes.color)} 
        if not color[1] then 
            color = {255,255,255} 
        end 
        teams[name] = attributes 
        teams[name].color = color 
        if not toboolean(get("noEmptyTeams")) then 
            teams[name].team = createTeam(name,unpack(color)) 
        end 
    end 
    for k,v in pairs(getElementsByType("player")) do 
        check(v) 
    end 
end 
addEventHandler("onResourceStart",getResourceRootElement(),initiate) 
  
--------------- 
-- Functions -- 
--------------- 
  
--- 
-- Checks the player's nick and ACL Groups and sets his team if necessary. 
-- 
-- @param   player   player: The player element 
-- 
function check(player) 
    if not isElement(player) or getElementType(player) ~= "player" then 
        debug("No player") 
        return 
    end 
    local nick = getPlayerName(player) 
    local accountName = getAccountName(getPlayerAccount(player)) 
    for name,data in pairs(teams) do 
        local tagMatch = false 
        local aclGroupMatch = false 
        if data.tag ~= nil and string.find(nick,data.tag,1,true) then 
            tagMatch = true 
        end 
        if data.aclGroup ~= nil and accountName and isObjectInACLGroup("user."..accountName,aclGetGroup(data.aclGroup)) then 
            aclGroupMatch = true 
        end 
        if data.required == "both" then 
            if tagMatch and aclGroupMatch then 
                addPlayerToTeam(player,name) 
                return 
            end 
        else 
            if tagMatch or aclGroupMatch then 
                addPlayerToTeam(player,name) 
                return 
            end 
        end 
    end 
    removePlayerFromTeam(player) 
    setPlayerTeam(player,nil) 
end 
  
--- 
-- Adds a player to the team appropriate for the name. 
-- It is not checked if the team is really defined in the table, since 
-- it should only be called if it is. 
-- 
-- Creates the team if it doesn't exist. 
-- 
-- @param   player   player: The player element 
-- @param   string   name: The name of the team 
-- 
function addPlayerToTeam(player,name) 
    local team = teams[name].team 
    if not isElement(team) or getElementType(team) ~= "team" then 
        team = createTeam(teams[name].name,unpack(teams[name].color)) 
        teams[name].team = team 
    end 
    setPlayerTeam(player,team) 
    debug("Added player '"..getPlayerName(player).."' to team '"..name.."'") 
    local attached = getAttachedElements (player) 
    local redBar, greenBar, blueBar = getTeamColor(team) 
    if (attached) then 
    for k,element in ipairs(attached) do 
    if getElementType (element) == "blip" then 
    setBlipColor(element,redBar,greenBar,blueBar,255) 
            end 
        end 
    end  
end 
  
--- 
-- Removes a player from a team. Also checks if any team 
-- needs to be removed. 
-- 
-- @param   player   player: The player element 
-- 
function removePlayerFromTeam(player) 
    setPlayerTeam(player,nil) 
    debug("Removed player '"..getPlayerName(player).."' from team") 
    if toboolean(get("noEmptyTeams")) then 
        for k,v in pairs(teams) do 
            local team = v.team 
            if isElement(team) and getElementType(team) == "team" then 
                if countPlayersInTeam(team) == 0 then 
                    destroyElement(team) 
                end 
            end 
        end 
    end 
end 
  
--- 
-- Converts a string-boolean into a boolean. 
-- 
-- @param   string   string: The string (e.g. "false") 
-- @return  true/false       Returns false if the string is "false" or evaluates to false (nil/false), true otherwise 
-- 
function toboolean(string) 
    if string == "false" or not string then 
        return false 
    end 
    return true 
end 
  
----------- 
-- Debug -- 
----------- 
  
-- Little debug function to turn on/off debug 
setElementData(getResourceRootElement(),"debug",true) 
function debug(string) 
    if getElementData(getResourceRootElement(),"debug") then 
        outputDebugString("autoteams: "..string) 
    end 
end[/code] 
  
  
and I think the error is here 
  
[code=text]function addPlayerToTeam(player,name) 
    local team = teams[name].team 
    if not isElement(team) or getElementType(team) ~= "team" then 
        team = createTeam(teams[name].name,unpack(teams[name].color)) 
        teams[name].team = team 
    end 
    setPlayerTeam(player,team) 
    debug("Added player '"..getPlayerName(player).."' to team '"..name.."'") 
    local attached = getAttachedElements (player) 
    local redBar, greenBar, blueBar = getTeamColor(team) 
    if (attached) then 
    for k,element in ipairs(attached) do 
    if getElementType (element) == "blip" then 
    setBlipColor(element,redBar,greenBar,blueBar,255) 
            end 
        end 
    end  
end 

Edited by Guest
Posted
------------ 
-- Events -- 
------------ 
  
function playerJoined() 
    check(source) 
end 
addEventHandler("onPlayerJoin",getRootElement(),playerJoined) 
  
function playerChangedNick(oldNick,newNick) 
    -- Use timer to wait until the nick really has changed 
    setTimer(check,1000,1,source) 
end 
addEventHandler("onPlayerChangeNick",getRootElement(),playerChangedNick) 
  
function playerQuit() 
    removePlayerFromTeam(source) 
end 
addEventHandler("onPlayerQuit",getRootElement(),playerQuit) 
  
-- Check for ACL Groups on login/logout 
function loggedIn() 
    check(source) 
end 
addEventHandler("onPlayerLogin",getRootElement(),loggedIn) 
  
function loggedOut() 
    check(source) 
end 
addEventHandler("onPlayerLogout",getRootElement(),loggedOut) 
  
  
-- Holds the teams as defined in the settings 
local teams = {} 
  
--- 
-- Reads the settings and creates the teams if enabled. 
-- 
function initiate() 
    local rootNode = getResourceConfig("config.xml") 
    local children = xmlNodeGetChildren(rootNode) 
    if children == false then 
        return 
    end 
    for _,node in pairs(children) do 
        local attributes = xmlNodeGetAttributes(node) 
        local name = attributes.name 
        local color = {getColorFromString(attributes.color)} 
        if not color[1] then 
            color = {255,255,255} 
        end 
        teams[name] = attributes 
        teams[name].color = color 
        if not toboolean(get("noEmptyTeams")) then 
            teams[name].team = createTeam(name,unpack(color)) 
        end 
    end 
    for k,v in pairs(getElementsByType("player")) do 
        check(v) 
    end 
end 
addEventHandler("onResourceStart",getResourceRootElement(),initiate) 
  
--------------- 
-- Functions -- 
--------------- 
  
--- 
-- Checks the player's nick and ACL Groups and sets his team if necessary. 
-- 
-- @param   player   player: The player element 
-- 
function check(player) 
    if not isElement(player) or getElementType(player) ~= "player" then 
        debug("No player") 
        return 
    end 
    local nick = getPlayerName(player) 
    local accountName = getAccountName(getPlayerAccount(player)) 
    for name,data in pairs(teams) do 
        local tagMatch = false 
        local aclGroupMatch = false 
        if data.tag ~= nil and string.find(nick,data.tag,1,true) then 
            tagMatch = true 
        end 
        if data.aclGroup ~= nil and accountName and isObjectInACLGroup("user."..accountName,aclGetGroup(data.aclGroup)) then 
            aclGroupMatch = true 
        end 
        if data.required == "both" then 
            if tagMatch and aclGroupMatch then 
                addPlayerToTeam(player,name) 
                return 
            end 
        else 
            if tagMatch or aclGroupMatch then 
                addPlayerToTeam(player,name) 
                return 
            end 
        end 
    end 
    removePlayerFromTeam(player) 
    setPlayerTeam(player,nil) 
end 
  
--- 
-- Adds a player to the team appropriate for the name. 
-- It is not checked if the team is really defined in the table, since 
-- it should only be called if it is. 
-- 
-- Creates the team if it doesn't exist. 
-- 
-- @param   player   player: The player element 
-- @param   string   name: The name of the team 
-- 
function addPlayerToTeam(player,name) 
    local team = teams[name].team 
    if not isElement(team) or getElementType(team) ~= "team" then 
        team = createTeam(teams[name].name,unpack(teams[name].color)) 
        teams[name].team = team 
    end 
    setPlayerTeam(player,team) 
    debug("Added player '"..getPlayerName(player).."' to team '"..name.."'") 
    local attached = getAttachedElements (player) 
    local redBar, greenBar, blueBar = unpack(teams[name].color) 
    if (attached) then 
    for k,element in ipairs(attached) do 
    if getElementType (element) == "blip" then 
    setBlipColor(element,redBar,greenBar,blueBar,255) 
            end 
        end 
    end  
end 
  
--- 
-- Removes a player from a team. Also checks if any team 
-- needs to be removed. 
-- 
-- @param   player   player: The player element 
-- 
function removePlayerFromTeam(player) 
    setPlayerTeam(player,nil) 
    debug("Removed player '"..getPlayerName(player).."' from team") 
    if toboolean(get("noEmptyTeams")) then 
        for k,v in pairs(teams) do 
            local team = v.team 
            if isElement(team) and getElementType(team) == "team" then 
                if countPlayersInTeam(team) == 0 then 
                    destroyElement(team) 
                end 
            end 
        end 
    end 
end 
  
--- 
-- Converts a string-boolean into a boolean. 
-- 
-- @param   string   string: The string (e.g. "false") 
-- @return  true/false       Returns false if the string is "false" or evaluates to false (nil/false), true otherwise 
-- 
function toboolean(string) 
    if string == "false" or not string then 
        return false 
    end 
    return true 
end 
  
----------- 
-- Debug -- 
----------- 
  
-- Little debug function to turn on/off debug 
setElementData(getResourceRootElement(),"debug",true) 
function debug(string) 
    if getElementData(getResourceRootElement(),"debug") then 
        outputDebugString("autoteams: "..string) 
    end 
end 
  

Ingame Name : |DGT|Puma

DGT Clan Server 24/7 Owner/Scripter

MultiGameMode in progress :

n-560x95_FFFFFF_FFFFFF_0283C4_000000.png

Posted

i tested it and work when the resource is started when the next map start all blips are white

Ingame Name : |DGT|Puma

DGT Clan Server 24/7 Owner/Scripter

MultiGameMode in progress :

n-560x95_FFFFFF_FFFFFF_0283C4_000000.png

Posted

Please don't double post and here:

  
function(player) 
if player then 
getPlayerTeam(player) 
 if (getPlayerTeam(player) == false) then 
-- OPEN THE GUI OR WHAT EVERY, THEN WHEN THEY CHOOSE A TEAM USE 
setPlayerTeam(player,"THE TEAM") 
local r, g, b = getTeamColor(getPlayerTeam(player)) 
createBlipAttachedTo(player,2,1,r,g,b) 
end 
end 
 end 

My in-game name: Jaysds1

Retired CMG Scripter

World Of Tanks GameMode (Open-Source): https://github.com/Jaysds1/mtasa-wot-gamemode

Online GUI-Editor (WIP): https://forum.mtasa.com/topic/47678-online-gui-editor/

 

sE5Qm.png

TiV3C.png

img.php?id=0&text=Lua%20Scripter

Posted (edited)

That's wrong.

function(player) 
if player then 
getPlayerTeam(player) 
 if (getPlayerTeam(player) == false) then 
-- OPEN THE GUI OR WHAT EVERY, THEN WHEN THEY CHOOSE A TEAM USE 
setPlayerTeam(player,getTeamFromName("THE TEAM")) 
local r, g, b = getTeamColor(getPlayerTeam(player)) 
createBlipAttachedTo(player,2,1,r,g,b) 
end 
end 
 end 

Edited by Guest

Business System viewtopic.php?f=108&t=35797

Notepad++ Syntax Highlighting & Auto Completion viewtopic.php?f=91&t=76726

SQLite Tutorial viewtopic.php?f=148&t=38203

Posted
That's wrong.
function(player) 
if player then 
getPlayerTeam(player) 
 if (getPlayerTeam(player) == false) then 
-- OPEN THE GUI OR WHAT EVERY, THEN WHEN THEY CHOOSE A TEAM USE 
setPlayerTeam(player,getTeamFromName("THE TEAM")) 
local r, g, b = getTeamColor(getPlayerTeam(player)) 
createBlipAttachedTo(player,2,1,r,g,b) 
end 
end 
 end 

Where I put this? thanks for replying all

Posted
function setThePlayerTeam(player) 
if player then 
getPlayerTeam(player) 
 if (getPlayerTeam(player) == false) then 
-- OPEN THE GUI OR WHAT EVERY, THEN WHEN THEY CHOOSE A TEAM USE 
setPlayerTeam(player,getTeamFromName("THE TEAM")) 
local r, g, b = getTeamColor(getPlayerTeam(player)) 
createBlipAttachedTo(player,2,1,r,g,b) 
end 
end 
 end 

Put it anywhere, and use setThePlayerTeam(player) anywhere you want to sets the player team to THE TEAM.

Business System viewtopic.php?f=108&t=35797

Notepad++ Syntax Highlighting & Auto Completion viewtopic.php?f=91&t=76726

SQLite Tutorial viewtopic.php?f=148&t=38203

Posted
function setThePlayerTeam(player) 
if player then 
getPlayerTeam(player) 
 if (getPlayerTeam(player) == false) then 
-- OPEN THE GUI OR WHAT EVERY, THEN WHEN THEY CHOOSE A TEAM USE 
setPlayerTeam(player,getTeamFromName("THE TEAM")) 
local r, g, b = getTeamColor(getPlayerTeam(player)) 
createBlipAttachedTo(player,2,1,r,g,b) 
end 
end 
 end 

Put it anywhere, and use setThePlayerTeam(player) anywhere you want to sets the player team to THE TEAM.

I do not understand what the "THE TEAM"

Posted
where I place it? thanks for replying
function setThePlayerTeam(player) 
if player then 
local r, g, b = getTeamColor(getPlayerTeam(player)) 
createBlipAttachedTo(player,2,1,r,g,b) 
end 
end 

Ingame Name : |DGT|Puma

DGT Clan Server 24/7 Owner/Scripter

MultiGameMode in progress :

n-560x95_FFFFFF_FFFFFF_0283C4_000000.png

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