local sampev = require('lib.samp.events');
local targetText = 'SpaceFlight';
---@param str string
---@param pos number
---@param startColor? string
---@return string?
function string.getColorAtPos(str, pos, startColor)
local result, parts, charIndex = nil, { [0] = startColor or '{ffffff}' }, 0;
for char in str:gmatch('.') do
charIndex = charIndex + 1;
if (char == '{') then
local colorTag = str:sub(charIndex, charIndex + 7):match('{%x+}');
if (colorTag) then
parts[charIndex] = colorTag;
end
end
end
for partIndex, partColorTag in pairs(parts) do
if (pos >= partIndex) then
result = partColorTag:match('{(%x+)}');
end
end
return result or (startColor or 'ffffff');
end
function sampev.onServerMessage(color, text)
local targetWordIndex = text:find(targetText);
if (targetWordIndex) then
local textColor = string.getColorAtPos(text, targetWordIndex);
local text = text:gsub(targetText, ('{ff0000}%s{%s'):format(targetText, textColor));
return { color, text };
end
end