Jump to content

Send group message error


thund3rbird23

Recommended Posts

I want to check if the player is in a specific group and if so then gets a message.

I have an isPlayerInGroup function:

function isPlayerInGroup(player, groupId)
	if isElement(player) and groupId then
		groupId = tonumber(groupId)

		if groupId and availableGroups[groupId] then
			local playerGroups = getElementData(player, "player.groups") or {}
			
			if playerGroups and playerGroups[groupId] then
				return true
			end
		end
	end

	return false
end

and how can I send to all players who are in example group id 1? this code is working with localPlayer but then just get the message who are clicked on the button.
 

if exports.s_groups:isPlayerInGroup(localPlayer,1) then
outputChatBox("Test")
end

 

Link to post

why you export the function you can just 

function isPlayerInGroup(player, groupId)
	if isElement(player) and groupId then
		groupId = tonumber(groupId)

		if groupId and availableGroups[groupId] then
			local playerGroups = getElementData(player, "player.groups") or {}
			
			if playerGroups and playerGroups[groupId] then
				return true
			end
		end
	end

	return false
end

function text()
  if isPlayerInGroup(localplayer,1) then
    outputChatBox("Test")
    end
end

text()

 

if the code have error show me the debug

Link to post
7 hours ago, salh said:

why you export the function you can just 


function isPlayerInGroup(player, groupId)
	if isElement(player) and groupId then
		groupId = tonumber(groupId)

		if groupId and availableGroups[groupId] then
			local playerGroups = getElementData(player, "player.groups") or {}
			
			if playerGroups and playerGroups[groupId] then
				return true
			end
		end
	end

	return false
end

function text()
  if isPlayerInGroup(localplayer,1) then
    outputChatBox("Test")
    end
end

text()

 

if the code have error show me the debug

Because it's in another Lua file and I want to call in client side and the isPlayerInGroup is server side.

As I said the function works but I want to send that message to all players who are in the ID 1 group not for only the localPlayer.

Link to post
3 hours ago, thund3rbird23 said:

As I said the function works but I want to send that message to all players who are in the ID 1 group not for only the localPlayer.

The variable name localPlayer isn't suppose to be used on serverside, because it only makes sense for the player that runs the MTA application and the server on his own pc. No other player in your server will be able to met that condition.

Note: If this code is fully clientside, you will not be able to send a message to other players.

 

Do you remember this loop you posted in your other topic?

It will allow you to go through all the players in the server.

 

 

 

 

Link to post
3 minutes ago, IIYAMA said:

The variable name localPlayer isn't suppose to be used on serverside, because it only makes sense for the player that runs the MTA application and the server on his own pc. No other player in your server will be able to met that condition.

I know what's localPlayer means. Just wrote that in the function for illustrate the code works with localPlayer but I need to send to all players who are in the ID 1 group.
 

6 minutes ago, IIYAMA said:

Note: If this code is fully clientside, you will not be able to send a message to other players.

Okay, so the function isPlayerInGroup is in server side:

function isPlayerInGroup(player, groupId)
	if isElement(player) and groupId then
		groupId = tonumber(groupId)

		if groupId and availableGroups[groupId] then
			local playerGroups = getElementData(player, "player.groups") or {}
			
			if playerGroups and playerGroups[groupId] then
				return true
			end
		end
	end

	return false
end

And I think I need to write another server side function (?):

function sendGroupMessage(groupId, msg)
	for k, v in ipairs(getElementsByType("player")) do
		if isPlayerInGroup(v, tonumber(groupId)) then
			outputChatBox("#F9BF3B[" .. availableGroups[groupId] .. "]#ffffff " .. msg, v, 255, 255, 255, true)
		end
	end
end
addEvent("sendGroupMessage", true)
addEventHandler("sendGroupMessage", root, sendGroupMessage)

And after that I need to trigger server event:

 triggerServerEvent("sendGroupMessage", 1, "Test")

Or I'm totally wrong?

Link to post
54 minutes ago, thund3rbird23 said:

Or I'm totally wrong?

By just looking at the initial setup, it looks very logic. That is without looking at syntax and any other issues at all.

Link to post
1 hour ago, IIYAMA said:

By just looking at the initial setup, it looks very logic. That is without looking at syntax and any other issues at all.

If I triggering the server event:

triggerServerEvent("sendGroupMessage", root, "Test")

Then don't gets errors... but if I debug the sendGroupMessage function, then prints: "elem:player[Jeff] 1 Test nil"
 

function sendGroupMessage(groupId, msg)
	for k, v in ipairs(getElementsByType("player")) do
	iprint(v,k,groupId,msg)
		if exports.s_groups:isPlayerInGroup(v, tonumber(groupId)) then
			outputChatBox("#F9BF3B[" .. availableGroups[groupId] .. "]#ffffff " .. msg, v, 255, 255, 255, true)
		end
	end
end
addEvent("sendGroupMessage", true)
addEventHandler("sendGroupMessage", root, sendGroupMessage)

So I don't get anything in the chatbox and the groupId returns the message which provided in the triggerServerEvent instead of the groupid and the msg is nil.

Edited by thund3rbird23
Link to post
3 hours ago, IIYAMA said:

Where is that suppose to come from? It is not as if groupid is something from default MTA.

That's come from another Lua file. But eh, too difficult to make in this way, I think it's have an easier way to do that.

Let's say I have 3 groups: 1, Police, 2: Government, 3: Maffia and if the player in the police group then I want to send him a message but for everyone who are in that group, not for only one or for only localplayer.

Link to post

Okay, finally a few hours I figured out how it works. But thank you all for your time and replies.

function sendGroupMessage(msg)
for index, player in ipairs (getElementsByType("player")) do 
		if exports["s_groups"]:isPlayerInGroup(player, 1) then 
		outputChatBox("#F9BF3B[Police]#ffffff " .. msg, player, 255, 255, 255, true)
		end
	end
end	
addEvent("sendGroupMessage", true)
addEventHandler("sendGroupMessage", getRootElement(), sendGroupMessage)

 

  • Like 1
Link to post

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