Hey there! I have a tagcolor and custom tag system in my game that allows special users to have different chat and tag colors utilizing their player ID. Recently, I've been trying to script a VIP shirt that will also grant these benefits, but after finding a way to try and do it, I get the error that I am "Unable to cast value to object" and it gives me Line 12, which I will label below
local plr = game.Players.LocalPlayer
local asset = 7246618473 --ShirtID
local plrs = game.Players
local sss = game.ServerScriptService
local chatService = require(sss:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))
chatService.SpeakerAdded:Connect(function(plr)
local speaker = chatService:GetSpeaker(plr)
if game:GetService("MarketplaceService"):PlayerOwnsAsset(plr,asset) then--says Script 'ServerScriptService.Script', Line 12
print("Player owns shirt")
speaker:SetExtraData('ChatColor', Color3.fromRGB(255, 255, 255))
speaker:SetExtraData('Tags', {{TagText = 'Donator', TagColor = Color3.fromRGB(255, 255, 255)}})
else
print("Player does not own shirt")
end
end)
I have never gotten this error before and everything about the script seems okay besides the error. It infact still prints that I own the shirt, but doesn't change the chatcolors or add the tag. Any help would be appreciated, thank you very much!

5 comments
Try putting it on another services (replicatedstorage, workspace etc...)
Putting it in ReplicatedStorage instead of ServerScriptService stops the error from occuring, but it still doesn't change the textcolor. It gives no errors anymore though however!
local plrs = game.Players is completely redundant if you're not going to use that variable.
:PlayerOwnsAsset requires a player as an argument. Plr is an object since it contains information from localplayer. LocalPlayer is an object/instance.
Ideally, this should be a reference to the player not the localplayer, since localplayer doesn't take any arguments/parameters