Jump to content

[CHAT] Trigger an Event


Recommended Posts

Hi there, I'm trying to trigger a client side event for every team player but excluding myself, within the team.
I've tried a bunch of things, can't seem to figure it out at this point. The code we're using today is only intended for test purposes.

Our client event "runOurClientEvent" is currently being triggered for everyone in the team, including myself.

function teamChatZm( message, messageType )

	local thePlayer = getPlayerName( source )

	if messageType == 2 then  -- Teamsay
		local playerTeam = getPlayerTeam( source )
		if ( playerTeam ) then
		local teamPlayers = getPlayersInTeam( playerTeam )
		cancelEvent()
		outputChatBox( "*Message type: " .. messageType .. " from: " .. thePlayer )
		outputChatBox( "Castiel has triggered a client side event.", teamPlayers, 0, 255, 0, true )

		-- trigger an event for the other team players only

		if ( teamPlayers[source] == source ) then
		  return else
		  triggerClientEvent ( "runOurClientEvent", root )
	 	end

		end
	end
end
addEventHandler("onPlayerChat", root, teamChatZm)

 

Link to comment
  • Moderators
2 hours ago, MTA.Castiel said:

Our client event "runOurClientEvent" is currently being triggered for everyone in the team, including myself.

Try this:

triggerClientEvent ( 
    (function () 
        local players = getElementsByType("player")
        for i=1, #players do
            if players[i] == source then
                table.remove(players, i)
                break
            end
        end
        return players
    end)(), --  IIFE (Immediately Invoked Function Expression)
    "runOurClientEvent", root 
)

 

  • Thanks 1
Link to comment
function teamChatZm( message, messageType )
	local thePlayer = getPlayerName( source )
	if messageType == 2 then  -- Teamsay
		local playerTeam = getPlayerTeam( source )
		if ( playerTeam ) then
			local teamPlayers = getPlayersInTeam( playerTeam )
			
			cancelEvent()
			
			outputChatBox( "*Message type: " .. messageType .. " from: " .. thePlayer )
			outputChatBox( "Castiel has triggered a client side event.", teamPlayers, 0, 255, 0, true )
	
			for i, v in ipairs (teamPlayers) do
				if v ~= source then
					triggerClientEvent (v, "runOurClientEvent", root )
				end
			end
		end
	end
end
addEventHandler("onPlayerChat", root, teamChatZm)

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