Jump to content

Chatbox message coloring problem


Recommended Posts

Hi, I've got an interesting bug with my localchat script. When the players are in range of local chat then for some of them a *action text* part is not colored, but when they're alone in their chat distance, everything works fine and message is fully colored.
 

	function RGBToHex(red, green, blue, alpha)
    
    -- Make sure RGB values passed to this function are correct
    if( ( red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255 ) or ( alpha and ( alpha < 0 or alpha > 255 ) ) ) then
        return nil
    end
	    -- Alpha check
    if alpha then
        return string.format("#%.2X%.2X%.2X%.2X", red, green, blue, alpha)
    else
        return string.format("#%.2X%.2X%.2X", red, green, blue)
    end
	end
	function localchat (msg, msgtype, cmdname)
    local x, y, z = getElementPosition (source)
    local playerdim = getElementDimension (source)
    local range = 30
    local playerName = getPlayerName (source)
        for index, player in pairs(getElementsByType ("player")) do 
            local playersdim = getElementDimension (player)
                if playerdim == playersdim then 
                    local xp, yp, yz = getElementPosition (player)
                    local distance = getDistanceBetweenPoints3D (x, y, z, xp, yp, yz)
                        if  ( distance <= range ) then
                            r = tonumber (255 - (tonumber((distance))* 3))
                            g = tonumber (255 - (tonumber((distance))* 3))
                            b = tonumber (255 - (tonumber((distance))* 3))
                            mr = tonumber (255 - (tonumber((distance))* 3))
                            mg = 0
                            mb = tonumber (255 - (tonumber((distance))* 3))
                            if msgtype == 0 then
                                cancelEvent()
                                msg = string.gsub(msg, "#%x%x%x%x%x%x", "")
                                msg = msg:gsub("^%l", string.upper)
                                playerName = string.gsub(playerName, "#%x%x%x%x%x%x", "")
                                playerName = string.gsub(playerName, "_", " ")
                                mecolor = RGBToHex(mr, mg, mb)
                                normalcolor = RGBToHex(r, g, b)
                                while true do
                                        local find,find2 = string.find(msg, "%<(.-)%>")
                                            if (find and find2) then
                                                msg = string.sub(msg,1,find-1) .. mecolor .. "*".. string.sub(msg, find+1, find2-1) .. "*" .. normalcolor .. string.sub(msg, find2+1, #msg)
                                                else
                                                    break
                                            end
                                end
                                    if string.find(msg, ".", -1, true) or string.find(msg, "?", -1, true) or string.find(msg, "!", -1, true) then
                                                    outputChatBox(playerName .. " mówi: " .. msg , player, r, g, b, true)
                                            else
                                                    msg = msg.. "."
                                                    outputChatBox(playerName .. " mówi: " .. msg , player, r, g, b, true)
                                                    --outputChatBox(playerName .. " mówi: " .. msg .. ".", player, 255, 255, 255, true)
                                    end
                            end
                        end
                end
        end
end
addEventHandler("onPlayerChat", getRootElement(), localchat)
Edited by Mefisto_PL
Link to comment
  • Moderators

When using the variables like playerName inside of the loop, you will have to redeclare the as a local variable in order to prevent them from being overwritten. (The variable name can be the same)

 

local playerName = "iiyama"

do -- in your case your loop
  local playerName = playerName .. "-alex" -- modifi
  iprint(playerName) -- iiyama-alex
end

iprint(playerName) -- iiyama

 

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