My VIP nametag script isn't working

I’m trying to make it where if you have a pass, you get a nametag in chat. One script is a local script in StarterPlayerScripts, and the other is a script in ServerScriptService. Here’s the local one that is supposed to add the nametag:

local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local MarketplaceService = game:GetService("MarketplaceService")

TextChatService.OnIncomingMessage = function(message:TextChatMessage)
	local properties = Instance.new("TextChatMessageProperties")
	
	if message.TextSource then
		local sender = Players:GetPlayerByUserId(message.TextSource.UserId)
		local hasPass = sender:GetAttribute("IsVIP")
		if hasPass then
			print("player has pass")
			properties.PrefixText = "<font color='#F5CD30'>[VIP]</font> " ..message.PrefixText
		end
	end
	return properties
end

Here’s the server one that sets if you’re VIP or not:

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

Players.PlayerAdded:Connect(function(plr)
	local hasVIP = plr:GetAttribute("IsVIP")
	local success, message = pcall(function()
		hasVIP = MarketplaceService:UserOwnsGamePassAsync(plr.UserId, 95142223)
	end)
	if not success then
		print("Error checking VIP:"..message)
	end
	print(hasVIP)
end)

It says that I have the pass, but it won’t give me the nametag in chat. How do I fix this?

if MarketplaceService:UserOwnsGamePassAsync(Player, "Your Pass") then
-- code here
else
-- else code here
end

1 Like
local gamepassId = 17747602 --Put your Gamepass ID here
local service = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
    if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
        local tags = {
            {
                TagText = "👑VIP",
                TagColor = Color3.fromRGB(255, 220, 20)
            }
        }
        local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService)
        local speaker = nil
        while speaker == nil do
            speaker = ChatService:GetSpeaker(player.Name)
            if speaker ~= nil then break end
            wait(0.01)
        end
        speaker:SetExtraData("Tags",tags)
        speaker:SetExtraData("ChatColor",Color3.fromRGB(226, 226, 0))
    end
end)

This should work if you are VIP when you have a gamepass.

2 Likes

@62penguins

You are setting the attribute incorrectly on the server.

Change the server script to this and see if it works:

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

Players.PlayerAdded:Connect(function(plr)
    local hasVIP = false -- Your mistake is getting the attribute when it hasn't been set yet
	local success, message = pcall(function()
		hasVIP = MarketplaceService:UserOwnsGamePassAsync(plr.UserId, 95142223)
	end)
	if not success then
		print("Error checking VIP:"..message)
        return
	end
    plr:SetAttribute("IsVIP", hasVIP)
end)


The UserOwnsGamePassAsync method should always be wrapped around a pcall.

The original poster is using TextChatService, not ChatService. You also aren’t wrapping the UserOwnsGamePassAsync method around a pcall.

4 Likes

Im pretty sure pcall is a error handler, im not sure how that will help

1 Like

Yes, and when using methods that make web requests (like UserOwnsGamePassAsync), a pcall is necessary because there is always a chance that it will fail if the web request isn’t successful. No one wants to join their game to see their gamepass(es) not loading for them.

Without a pcall, if the web request was unsuccessful, the rest of the code in the script will not execute which is very bad.

Wrapping it around a pcall will allow of the rest of the code in to execute, if the request were to fail.

1 Like

Hmm. It’s still not working, but I don’t know why. I’ve done a little more research on TextChatService, and in theory, this should work. :thinking:

2 Likes

Are there any errors in the output?

No, there are no errors. Let me check the console and maybe there’ll be something there.

Strange, i never have these issues

I wasn’t finding anything. There was some stuff about chat scripts in there, so I might have to look at that later, but there wasn’t much sticking out to me.

So my script won’t work, that’s it?

It works for ChatService (old roblox chat), but not for TextchatService, which is the new chat that roblox recently released.

2 Likes

I had the same problem, this might help (This is for the new chat service)

Oh my goodness, thank you so much for mentioning that. :sweat_smile: I didn’t have the new TextChatService enabled. :man_facepalming:

1 Like

It’s fine! We all make mistakes. Happy to help :slight_smile:

1 Like

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