Jump to content

onPlayerChat help


Ernoso

Recommended Posts

Hello, guys! I have a simple problem in my groupsystem, there is a problem sending messages on teamsay, I'm trying to use cancelEvent and outputChatBox but the message doesn't appears on the chat, look at my function:

function getPlayersInsideGroup (group)
	for i, allplayers in ipairs(getElementsByType("player")) do 
	local pedGang = getElementData(allplayers,"Group")
	local theGang = group
		if ( pedGang == theGang ) then
			playersInside = allplayers
			return playersInside
		end
	end
end

function playerSentGroupMsg (message, messageType)
	cancelEvent()
	if ( messageType == 2 ) then
		if ( getElementData(source,"Group") ) then
			local grupinhdd = getElementData(source,"Group")
			local jogadores = getPlayersInsideGroup(grupinhdd)
			outputChatBox("[Group]"..getPlayerName(source)..": "..message,jogadores,0,255,0)
		end
	end
end
addEventHandler("onPlayerChat", getRootElement(), playerSentGroupMsg)

The message doesn't appears on the chat and there is no erros on console or debugscript, any help?

Edited by Ernoso
Link to comment

First the  getPlayersInsideGroup function will always return with one element:

function getPlayersInsideGroup (group)
	local gangMembers = {}
	for i, thePlayer in ipairs(getElementsByType("player")) do 
      	local playerGang = getElementData(thePlayer,"Group")
	    if ( playerGang == group ) then
	        table.insert(gangMembers, thePlayer)
	    end
	end
	return gangMembers
end

On the second i think cancelEvent() works like return so when the  playerSentGroupMsg will be called it will return immediately.

function playerSentGroupMsg (message, messageType)
	if ( messageType == 2 ) then
		if ( getElementData(source,"Group") ) then
			local grupinhdd = getElementData(source,"Group")
			local jogadores = getPlayersInsideGroup(grupinhdd)--You get an array of player
			for k,v in pairs(jogadores) do --You have to send them one by one
				outputChatBox("[Group]"..getPlayerName(source)..": "..message,v,0,255,0)
			end
		end
	end
	cancelEvent()
end
addEventHandler("onPlayerChat", getRootElement(), playerSentGroupMsg)

 

Link to comment
35 minutes ago, dugasz1 said:

First the  getPlayersInsideGroup function will always return with one element:


function getPlayersInsideGroup (group)
	local gangMembers = {}
	for i, thePlayer in ipairs(getElementsByType("player")) do 
      	local playerGang = getElementData(thePlayer,"Group")
	    if ( playerGang == group ) then
	        table.insert(gangMembers, thePlayer)
	    end
	end
	return gangMembers
end

On the second i think cancelEvent() works like return so when the  playerSentGroupMsg will be called it will return immediately.


function playerSentGroupMsg (message, messageType)
	if ( messageType == 2 ) then
		if ( getElementData(source,"Group") ) then
			local grupinhdd = getElementData(source,"Group")
			local jogadores = getPlayersInsideGroup(grupinhdd)--You get an array of player
			for k,v in pairs(jogadores) do --You have to send them one by one
				outputChatBox("[Group]"..getPlayerName(source)..": "..message,v,0,255,0)
			end
		end
	end
	cancelEvent()
end
addEventHandler("onPlayerChat", getRootElement(), playerSentGroupMsg)

 

Still not working, the message doesn't appears on the chat... :(

Link to comment

Another function to get the players in the same group isn't really necessary unless you need to do something else with them other that output a message.

Here's a simple way of doing it.

Used my phone to do this so format might be a bit off.

function playerSentGroupMsg (message, messageType)
    cancelEvent()
    if ( messageType == 2 ) then
        if ( getElementData(source,"Group") ) then
            local myGroup = getElementData(source,"Group") --store your group name in a variable.
            local plrs = getElementsByType"player"
            for i=1,#plrs do --now we loop through all the players so that we can output the message for each player.
                local plr = plrs[i]
                if getElementData(plr,"Group") == myGroup then -- this checks to see if the player's group name is the same as yours.
                    outputChatBox("[Group]"..getPlayerName(source)..": "..message,plr,0,255,0) -- now we output the message for each player that has the same group name as the sender.
                end
            end
	      end
    end
end
addEventHandler("onPlayerChat", root, playerSentGroupMsg)

 

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