Chat Prefix Help

Hello, I’m trying to achieve something like this for the developers and for people with the gamepass:

Right now it causes me not to see other players chats, I also get this error when I’m in a public server:
TextChatService.OnIncomingMessage:MarketPlace:UserOwnsGamePassAsync() can only query local player

Here is the local script in starterplayerscripts:

local developers = {"DinoViotto", "FizzyColas", "rvsita", "ashhchuu"}
local TextChatService = game:GetService("TextChatService")
local service = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamepassId = 25949990

TextChatService.OnIncomingMessage = function(message: TextChatMessage)
    local props = Instance.new("TextChatMessageProperties")

    if message.TextSource then
        local player = Players:GetPlayerByUserId(message.TextSource.UserId)

        if table.find(developers, player.Name) then
            props.PrefixText = "<font color='#e5d415'>[🔨] [Developer]</font> " .. message.PrefixText
        end
        if service:UserOwnsGamePassAsync(player.UserId, gamepassId) then
            props.PrefixText = "<font color='#d4af37'>[👑] [VIP]</font> " .. message.PrefixText
        end
    end

    return props    
end

Any help is appreciated!

2 Likes

Make it a normal script and place it into ServerScriptService.

Also, you should be using UserIds instead of your developer’s names so it will work even if any of your developer’s change their usernames.

Fixed code:

--//Services
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local TextChatService = game:GetService("TextChatService")

--//Controls
local gamepassId = 25949990

--//Tables
local developerIds = {
	295954212, 
	84694314, 
	84398600, 
	1655354023
}

--//Functions
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	local props = Instance.new("TextChatMessageProperties")

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)

		if table.find(developerIds, player.UserId) then
			props.PrefixText = "<font color='#e5d415'>[🔨] [Developer]</font> ".. message.PrefixText
		end
		
		if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
			props.PrefixText = "<font color='#d4af37'>[👑] [VIP]</font> ".. message.PrefixText
		end
	end

	return props    
end
1 Like

Hello! Thank you for your help. Unfortunately I get this error TextChatService.OnIncomingMessage can only be implemented on the client

I’m pretty sure it’s becuase textchatservice has to be used in a local script. Is there another way?

Oh, well :UserOwnsGamepassAsync has to be used on the server in order to access other player’s gamepass status. You need to invoke a remote function from your local script to the server so you can retrieve another player’s gamepass status.

If you need help implementing that, feel free to ask and I can write a script for you.

Hi!
I know for server to client I do this in a script (IsVIP is a remote event)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage:WaitForChild("IsVIP")

local function onPlayerAdded(player)
	remoteEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

but I’m not sure how to wait for the event to fire and then implement the vip chat prefix

Edit: You should try the other post I sent as it’s more efficient, don’t do this one.

Okay, I’ll tell you steps then.

  1. Make a LocalScript in StarterPlayerScripts and paste this inside:
--//Services
local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")

--//Variables
local VIPStatus = ReplicatedStorage.VIPStatus
VIPStatus.Name = HttpService:GenerateGUID(false)
VIPStatus.Parent = nil

--//Tables
local developerIds = {
	295954212, 
	84694314, 
	84398600, 
	1655354023
}

--//Functions
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	local props = Instance.new("TextChatMessageProperties")

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)

		if table.find(developerIds, player.UserId) then
			props.PrefixText = "<font color='#e5d415'>[🔨] [Developer]</font> ".. message.PrefixText
		end
		
		VIPStatus.Parent = ReplicatedStorage
		local isVIP = VIPStatus:InvokeServer(player)
		VIPStatus.Parent = nil
		
		if isVIP then
			props.PrefixText = "<font color='#d4af37'>[👑] [VIP]</font> ".. message.PrefixText
		end
	end

	return props    
end
  1. Make a new RemoteFunction in ReplicatedStorage named “VIPStatus”:
    image

  2. Add a new script into ServerScriptService and paste this inside:
    image

--//Services
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local VIPStatus = ReplicatedStorage.VIPStatus

--//Controls
local gamepassId = 25949990

--//Tables
local gamepassCache = {}

--//Functions
VIPStatus.OnServerInvoke = function(player, targetPlayer)
	if not targetPlayer or typeof(targetPlayer) ~= "Instance" or not targetPlayer:IsA("Player") then
		return
	end
	
	local hasGamepass = gamepassCache[targetPlayer.UserId] or MarketplaceService:UserOwnsGamePassAsync(targetPlayer.UserId, gamepassId)
	
	if not gamepassCache[targetPlayer.UserId] and hasGamepass then
		gamepassCache[targetPlayer.UserId] = true
	end
	
	return hasGamepass
end
  1. It should all work now. If there are any errors, please tell me them and I will fix it.

Another option would be to give the player an attribute called “Rank” or something on the server, then check for this rank on the client when a message is received. Then on the client append to the message as needed according to the user’s rank. This avoids having to call :UserOwnsGamepass every time a message is sent.

I amended a version for you, @FizzyColas I would suggest following this one instead because of the benefits.

  1. Make a LocalScript in StarterPlayerScripts and paste this inside:
--//Services
local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")

--//Tables
local developerIds = {
	295954212, 
	84694314, 
	84398600, 
	1655354023
}

--//Functions
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	local props = Instance.new("TextChatMessageProperties")

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)

		if table.find(developerIds, player.UserId) then
			props.PrefixText = "<font color='#e5d415'>[🔨] [Developer]</font> ".. message.PrefixText
		end
		
		if player:GetAttribute("VIP") then
			props.PrefixText = "<font color='#d4af37'>[👑] [VIP]</font> ".. message.PrefixText
		end
	end

	return props    
end
  1. Add a new script into ServerScriptService and paste this inside:
    image
--//Services
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

--//Controls
local vipGamepassId = 25949990

--//Functions
Players.PlayerAdded:Connect(function(player)
	player:SetAttribute("VIP", MarketplaceService:UserOwnsGamePassAsync(player.UserId, vipGamepassId))
end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, gamepassId, wasPurchased)
	if not wasPurchased or gamepassId ~= vipGamepassId then
		return
	end
	
	player:SetAttribute("VIP", true)
end)
  1. It should work now, if there are any errors, please tell me and I will fix it.
1 Like

this works, thank you very much. didn’t know attributes would come in handy for this

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.