[Help] Using regex to remove player color codes

Hello. Someone on Reddit very kindly gave an example of how to remove player color codes using regex, below is the example:

local name = “My~r~Red~g~Name~w~”

print(tostring(name:gsub("(~%w~)", “”)))

However, im unsure as to how to implement this into FiveM. Id like to use this code to remove players color codes upon joining, because people with rainbow names etc can be annoying, and alot of servers use colored names to represent staff etc. Im just unsure of the process of changing there name. Any help would be appreciated :slight_smile:

Something like this should work

local names = {}

local nametable = {
    ["~r~"] = "",
    ["~g~"] = "",
    ["~o~"] = "",
    ["~b~"] = "",
    ["~m~"] = "",
    ["~p~"] = "",
}

AddEventHandler("playerConnecting", function(name)
local src = source
    print("Name before: " .. name)
    for k,v in pairs(nametable) do
        name = string.gsub(name, k, v)
    end
    print("Name after: " .. name)
names[src] = name
end)

function GetNewPlayerName(id)
    for k,v in pairs(names) do
        if (k == id) then
            return v
        end
    end
    return GetPlayerName(id)
end

NOT TESTED

1 Like

Ok just i dropped it into a folder with a __resouce.lua, and put that code in a server.lua, but it doesnt seemed to have worked. Cant be far off tho as code looks great. Im no lua expert tho

Whats inside your __resource.lua?

Wait it works to an extent. In the console it is printing the correct name change, but just not in the server. Might be because i use rolesFX for the chat, and its using the old name? Im not sure

resource_manifest_version ‘77731fab-63ca-442c-a67b-abc70f28dfa5’

server_scripts{
“server.lua”
}

It does not change their actual name, that’s why the new function is included. Which grabs their “New Name”

Ohh yeh my bad i see now. Ok so i can use it to change the name i assume

The function GetNewPlayerName returns their new name

Yeh i see now ok thankyou