Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Custom Chat With Chat Tags Help?

I want to make a custom chat which i have succeed doing so far. However I want to make it so players who have purchased certain gamepasses have certain tags next to their names when they talk. For example if someone bought VIP when they say "hello world" in chat is would appear "[VIP] Player: hello world".

I have so far managed to make a chat tag next to everyones name by changing this line:

local sanitizedMessage = sender .. ": " .. game.Chat:FilterStringForPlayerAsync(message, player)

To this

local sanitizedMessage = "[VIP]" .. sender .. ": " .. game.Chat:FilterStringForPlayerAsync(message, player)

I'm just not sure on how to make a function that changes this if a player has a certain gamepass. The code so far:

The Chat: http://prntscr.com/8uqzyv

local messageData = {}
local MAX_MESSAGES = 10
----GamePass----
local passId = 0000000
----GamePass----

local function storeMessage(sender, message)
	for _, player in pairs(game.Players:GetPlayers()) do
		local sanitizedMessage = sender .. ": " .. game.Chat:FilterStringForPlayerAsync(message, player)
		local playerMessages = messageData[tostring(player.userId)]
--		table.insert(messageData[tostring(player.userId)], sanitizedMessage)
--		if #messageData[tostring(player.userId)] > MAX_MESSAGES then
--			table.remove(messageData[tostring(player.userId)], 1)
--		end
--		game.ReplicatedStorage.SendMessages:FireClient(player, messageData[tostring(player.userId)])
		table.insert(playerMessages, sanitizedMessage)
			if #playerMessages > MAX_MESSAGES then
				table.remove(playerMessages, 1)
			end
		game.ReplicatedStorage.SendMessages:FireClient(player, playerMessages)
	end
end

game.Players.PlayerAdded:connect(function(player)
	local playerMessages = {}
	messageData[tostring(player.userId)] = playerMessages
	
	player.Chatted:connect(function(message)
		storeMessage(player.Name,  message)
	end)
	
	player.CharacterAdded:connect(function(character)
		game.ReplicatedStorage.SendMessages:FireClient(player, messageData[tostring(player.userId)])
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	messageData[tostring(player.userId)] = nil
end)

The trigger: http://prntscr.com/8ur0s9 Chat Gui: http://prntscr.com/8ur0zp

local chatFrame = script.Parent.Frame

game.ReplicatedStorage.SendMessages.OnClientEvent:connect(function(messages)
	chatFrame:ClearAllChildren()	
	
	for i, message in pairs(messages) do
		local newLine = Instance.new("TextLabel", chatFrame)
		newLine.Text = message	
		newLine.Size = UDim2.new(1,0,0,18)
		newLine.Position = UDim2.new(0,0,0,18*(i-1))
		newLine.TextXAlignment = Enum.TextXAlignment.Left
		newLine.TextYAlignment = Enum.TextYAlignment.Center
		newLine.BackgroundTransparency = 1
		newLine.BorderSizePixel = 0
		newLine.FontSize = Enum.FontSize.Size18
		newLine.Font = Enum.Font.SourceSans
		print(i .. ": " .. message)
	end
end)

1 comment

0

When a player joins, you can use MarketplaceService:PlayerOwnsAsset(), and if it returns true, add tag

1 answer

✓ Accepted answer
local messageData = {}
local MAX_MESSAGES = 10
----GamePass----
local passId = 0000000
----GamePass----

AddTag = function(sender, msg)
	--Change the following to whatever you want...
	if game:GetService("MarketplaceService"):PlayerOwnsAsset(sender, 00000000) then --Change to ID
		return "[VIP] "..sender.Name..": "..msg
	end
	return sender.Name..": "..msg
end

local function storeMessage(sender, message)
    for _, player in pairs(game.Players:GetPlayers()) do
        local sanitizedMessage = AddTag(sender, game.Chat:FilterStringForPlayerAsync(message, player))
        local playerMessages = messageData[tostring(player.userId)]
--      table.insert(messageData[tostring(player.userId)], sanitizedMessage)
--      if #messageData[tostring(player.userId)] > MAX_MESSAGES then
--          table.remove(messageData[tostring(player.userId)], 1)
--      end
--      game.ReplicatedStorage.SendMessages:FireClient(player, messageData[tostring(player.userId)])
        table.insert(playerMessages, sanitizedMessage)
            if #playerMessages > MAX_MESSAGES then
                table.remove(playerMessages, 1)
            end
        game.ReplicatedStorage.SendMessages:FireClient(player, playerMessages)
    end
end

game.Players.PlayerAdded:connect(function(player)
    local playerMessages = {}
    messageData[tostring(player.userId)] = playerMessages

    player.Chatted:connect(function(message)
        storeMessage(player,  message)
    end)

    player.CharacterAdded:connect(function(character)
        game.ReplicatedStorage.SendMessages:FireClient(player, messageData[tostring(player.userId)])
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    messageData[tostring(player.userId)] = nil
end)

This should work, I've created a new function which checks if the player owns the asset, and if they do then it returns the message (with the [VIP] bit), otherwise it just returns the original message.

Be warned, I had to configure it so that the variable passed across to the storeMessage function was the player instance rather than the name, I'd recommend you check to see if this breaks anything.

This discussion is preserved as it was. New answers, comments, and votes are closed.