Jump to content

[Help] Players main chat color differentiation


Ice_Cool

Recommended Posts

Hello everyone, 

1. I am trying to differentiate the sender's chat color from receiver. The sender should see his text sent in a different color while everyone except him shall see in a different color. 

I tried to do this by creating two functions one for sender and one for receiver but the problem is the script end up showing double chat. 

Here is the code

function sendMessageToNearbyPlayers(message, messageType)
    if messageType == 0 then
		cancelEvent()
		local senderName = getPlayerName(source)
        local posX, posY, posZ = getElementPosition(source)
        local nearbyPlayers = getElementsWithinRange(posX, posY, posZ, 10, "player")
			
		if (source) == "nearbyPlayers" and not "senderName" then
			outputChatBox ( getPlayerName(source).." says: "..message, nearbyPlayers, 255, 255, 0, true )
			end
		end
    end
end
addEventHandler("onPlayerChat", getRootElement(), sendMessageToNearbyPlayers)

local function playerChat(message, messageType)
	if messageType == 0 then --Global (main) chat
                cancelEvent()
              
		outputChatBox(getPlayerName(source)..": s says: "..message, root, 255, 200, 200, true )
		
	end
end
addEventHandler("onPlayerChat", root, playerChat)

 2. Please guys also help me "how to define a local player to differentiate him from others" it would help me in my other scripts. 

Thanking you so much for your positive responses in advance!

Link to comment

Hello Ice_Cool,

do you know about for-loops in Lua? You can loop over all nearbyPlayers and send different colors for every item in nearbyPlayers if the item == source (the sender in onPlayerChat).

There are two things that make no sense in your existing code, namely...

if (source) == "nearbyPlayers" and not "senderName" then

... does always fail and...

			end

... is one end-token too many.

Edited by The_GTA
Link to comment

Thanks The_GTA i am thankful and I am almost there.

However, the problem with this for loop I get is that it returns 1 msg if i am alone in the server, and it returns the message twice (in two different colors) if there is one more player in game. 

I am a beginner scripter, if you instead of suggesting functions help me a bit by editing this code, It will really really be helpful and timesaving. 

function sendMessageToNearbyPlayers(message, messageType)
    if messageType == 0 then
		cancelEvent()
		senderName = getPlayerName(source)
        local posX, posY, posZ = getElementPosition(source)
        local nearbyPlayers = getElementsWithinRange(posX, posY, posZ, 10, "player")
		for key, item in pairs (nearbyPlayers) do
			if item == source then
				outputChatBox ( getPlayerName(source).." says: "..message, source, 255, 255, 255, true )
			else
				outputChatBox ( getPlayerName(source).." says: "..message, nearbyPlayers, 255, 255, 0, true )
			end
		end
    end
end
addEventHandler("onPlayerChat", getRootElement(), sendMessageToNearbyPlayers)

Thank you once again for your time, hope we can fix it this time. :) 

  • Like 1
Link to comment

It looks like you forgot to adjust the second parameter to outputChatBox which should be the element to send the message to. Did you know that you can simply use the item variable? The reason is that it points to each eligible player exactly one time. If you pass the nearbyPlayers variable which is a list of players then it sends the message in the color 255, 255, 0 to the nearbyPlayers multiplied by (#nearbyPlayers - 1).

Do this instead:

		-- This way the player who sent the message is guarranteed to always receive it independent of any math.
		outputChatBox ( getPlayerName(source).." says: "..message, source, 255, 255, 255, true )
		-- Send to all other nearby players now.
		for key, item in pairs (nearbyPlayers) do
			if not (item == source) then
				outputChatBox ( getPlayerName(source).." says: "..message, item, 255, 255, 0, true )
			end
		end

Those small errors happen to the best of us. ?

Edited by The_GTA
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...