Jump to content

Gang system in date element


Adiidas

Recommended Posts

Hello, I want to ask you for help. I'm trying to create a gang system but I do not want to use mta's default createTeam. I wanted to use an elementData, but I do not know how to do it. Tell me how to do in elementData. 

I have one with the default createTeam that is working, I wanted to change this by elementData.

function createGang(name, tag, gr, gg, gb)
	if getTeamFromName(name) then
		triggerClientEvent(source,"servers",source)
		outputChatBox("#666666[#B30A0AGang#666666] #FF0000Uma gang com esse nome já existe", source, 255, 255, 255, true)
		return
	else
		if name == "" or name == " " or tag == "" or tag == " " then
			triggerClientEvent(source,"servers",source)
			outputChatBox("#666666[#B30A0AGang#666666] #FF0000Você deve completar todos os campos pedidos para criar uma gang.", source, 255, 255, 255, true)
		else
			gangCreate = createTeam(name)
			setPlayerTeam(source, getTeamFromName(name))
			setTeamColor(gangCreate, gr, gg, gb)
			gang = getPlayerTeam(source)
			setElementData(source, "GangPTT", lider)
			if not getElementData(gang, "GangPlayer1") then
				setElementData(gang, "GangPlayer1", getPlayerName(source))
			end
			local r, g, b = getTeamColor (gang)
			local hex = string.format("#%.2X%.2X%.2X", r, g, b)
			triggerClientEvent(source,"servers",source)
			outputChatBox("#666666[#B30A0AGang#666666] #FFFFFFVocê criou a gang #666666[ "..hex..getTeamName(gang).." #666666]#FFFFFF e agora é lider dela.", source, 255, 255, 255, true)
		end
	end
end
addEvent("createGang", true)
addEventHandler("createGang", getRootElement(), createGang)

Spoiler

 

 

Link to comment

I did a code using the createElement function. To be honest, I don't know if it works 100% but by the small test I did it seems so.

local serverTeams = createElement( "server:customTeams" )

function CreateTeam( name, c )
	outputChatBox("@CreateTeam")
	return setElementData( serverTeams, tostring(name), 
			{ color = c, ID = (#getAllElementData(serverTeams) or 0) + 1, members = {}, friendlyFire = false }
	)
end

function SetPlayerTeam( player, teamName )
	if player and isElement(player) and teamName then
		if getElementData( serverTeams, tostring(teamName) ) then
			outputChatBox("@SetPlayerTeam ")
			if GetPlayerTeam( player ) and GetPlayerTeam( player ) ~= tostring(teamName) then
				updateTeamMembers( GetPlayerTeam( player ), player )
			end
			updateTeamMembers( tostring(teamName), player, true )
			
			return setElementData( serverTeams, tostring(name), 
					{ color = c, ID = id, members = {}, friendlyFire = ff }
			)
		end
	end
end

function GetPlayerTeam( player )
	if player and isElement(player) then
		for teamName, t in pairs( getAllElementData(serverTeams) ) do
			for i, p in pairs(t.members) do
				if player == p then
					return teamName
				end
			end
		end
	end
	return false
end

function updateTeamMembers( teamName, player, add )
	if teamName and getElementData( serverTeams, tostring(teamName) ) then
		local team = getElementData( serverTeams, tostring(teamName) )
		if type(team) ~= "table" then return false end;
		
		if add then
			table.insert( team.members, player )
			outputChatBox("table index 1: "..tostring(team.members[1]))
		else
			local membersTable = {}
			for i, p in pairs( team.members ) do
				outputChatBox("i: "..tostring(i).." p: "..tostring(getPlayerName(p)))
				if p ~= player then
					table.insert( membersTable, p )
				end
			end
		end
		return setElementData( serverTeams, tostring(teamName), 
				{ color = team.color, ID = team.ID, members = add and team.members or membersTable, friendlyFire = team.friendlyFire }
		)
	end
	return false
end

addEventHandler( "onPlayerQuit", root,
	function()
		if GetPlayerTeam( source ) then
			updateTeamMembers( GetPlayerTeam( source ), source )
		end
	end
)

addCommandHandler("setmyteam", function(p, cmd)
	SetPlayerTeam( p, "testTeam1" )
end)
addCommandHandler("getmyteam", function(p, cmd)
	outputChatBox( tostring( GetPlayerTeam(p) ) )
end)

addEventHandler( "onResourceStart", resourceRoot,
	function()
		CreateTeam( "testTeam1", {30, 240, 30} )
	end
)

Basically it works as the MTA's team system and It isn't finished of course.

Link to comment
12 hours ago, DNL291 said:

I did a code using the createElement function. To be honest, I don't know if it works 100% but by the small test I did it seems so.


local serverTeams = createElement( "server:customTeams" )

function CreateTeam( name, c )
	outputChatBox("@CreateTeam")
	return setElementData( serverTeams, tostring(name), 
			{ color = c, ID = (#getAllElementData(serverTeams) or 0) + 1, members = {}, friendlyFire = false }
	)
end

function SetPlayerTeam( player, teamName )
	if player and isElement(player) and teamName then
		if getElementData( serverTeams, tostring(teamName) ) then
			outputChatBox("@SetPlayerTeam ")
			if GetPlayerTeam( player ) and GetPlayerTeam( player ) ~= tostring(teamName) then
				updateTeamMembers( GetPlayerTeam( player ), player )
			end
			updateTeamMembers( tostring(teamName), player, true )
			
			return setElementData( serverTeams, tostring(name), 
					{ color = c, ID = id, members = {}, friendlyFire = ff }
			)
		end
	end
end

function GetPlayerTeam( player )
	if player and isElement(player) then
		for teamName, t in pairs( getAllElementData(serverTeams) ) do
			for i, p in pairs(t.members) do
				if player == p then
					return teamName
				end
			end
		end
	end
	return false
end

function updateTeamMembers( teamName, player, add )
	if teamName and getElementData( serverTeams, tostring(teamName) ) then
		local team = getElementData( serverTeams, tostring(teamName) )
		if type(team) ~= "table" then return false end;
		
		if add then
			table.insert( team.members, player )
			outputChatBox("table index 1: "..tostring(team.members[1]))
		else
			local membersTable = {}
			for i, p in pairs( team.members ) do
				outputChatBox("i: "..tostring(i).." p: "..tostring(getPlayerName(p)))
				if p ~= player then
					table.insert( membersTable, p )
				end
			end
		end
		return setElementData( serverTeams, tostring(teamName), 
				{ color = team.color, ID = team.ID, members = add and team.members or membersTable, friendlyFire = team.friendlyFire }
		)
	end
	return false
end

addEventHandler( "onPlayerQuit", root,
	function()
		if GetPlayerTeam( source ) then
			updateTeamMembers( GetPlayerTeam( source ), source )
		end
	end
)

addCommandHandler("setmyteam", function(p, cmd)
	SetPlayerTeam( p, "testTeam1" )
end)
addCommandHandler("getmyteam", function(p, cmd)
	outputChatBox( tostring( GetPlayerTeam(p) ) )
end)

addEventHandler( "onResourceStart", resourceRoot,
	function()
		CreateTeam( "testTeam1", {30, 240, 30} )
	end
)

Basically it works as the MTA's team system and It isn't finished of course.

Mas como eu exporto essa Gang para o scoreboard? 

Tentei assim mas não funcionou

exports["scoreboard"]:scoreboardAddColumn ( "server:customTeams", root, 20, "server:customTeams", 1 )

 

Link to comment
  • MTA Anti-Cheat Team

@Adiidas once you started asking a question in full english and appropiate English board, please don't switch to talking Portuguese in the midst.

One more portuguese reply and it would've been moved, if you want to ask a question (mainly) in Portuguese then start topic in that language's board please

 

  • Like 1
Link to comment
  • 3 weeks later...

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