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?
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.
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.
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.
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.