If you want to use a working version in your game, take this model: Chat For GUI [GAMEPASS VERSION]
I want to show a UI element when the player chats something and owns a gamepass. I've tried this script but doesn't seem to work. (I put this in a LocalScript in StarterGui and Script in workspace/ServerScriptService) Here's what happens: Click Here My problem seems to be (and I've tried without gamepass code) the player.Chatted.
local MarketplaceService = game:GetService("MarketplaceService") local Players = game:GetService("Players") local gamePassID = 9291233 local player = Players.LocalPlayer local hasPass = false hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) game.Players.PlayerAdded:connect(function(plr) plr.Chatted:connect(function(msg) if msg == "#custom" then if hasPass == false then MarketplaceService:PromptGamePassPurchase(player , gamePassID) else script.Parent.Custom.CUSTOMIZE.Visible = not script.Parent.Custom.CUSTOMIZE.Visible end end end) end)
I also tried
script.Parent.Custom.CUSTOMIZE.Visible = true
Nothing seems to work
I think I see a few errors.
1) You can't use LocalPlayer inside of a ServerScript as it doesn't run on the client.
2) You can't use game.Players.PlayerAdded inside of a LocalScript as the script doesn't run, because the script will first initialize upon being loaded into the game.
How To Fix This Problem
Use a ServerScript and get the player from when they added instead of using LocalPlayer.
Fixed Script
local MPS = game:GetService("MarketPlaceService") local passId = 9291233 game.Players.PlayerAdded:Connect(function(player) local hasPass = MPS:UserOwnsGamepassAsync(player.UserId, passId) player.Chatted:Connect(function(msg) if string.match(msg, "#custom", 1) then if hasPass == true then local pGui = player:WaitForChild("PlayerGui") local acG = pGui:WaitForChild("Name_Of_Screen_GUI_Here") acG.Custom.CUSTOMIZE.Visible = false --// Change This To The Directory Of The GUI Item You Want To Make It Non-Visible else MPS:PromptGamePassPurchase(player, passId) end end end) end)
Hope this helped! If you need an explanation or if this doesn't work, let me know!
Try pcalling this line
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
Use this site as a guide: https://developer.roblox.com/en-us/articles/Game-Passes-One-Time-Purchases
I figured it out! ServerScriptService (Script)
game.Players.PlayerAdded:Connect(function(player) player.Chatted:Connect(function(msg) if string.match(msg, "Message", 1) then game.ReplicatedStorage.RemoteEvent:FireClient(player) end end) end)
and StarterGui (LocalScript)
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() local MarketplaceService = game:GetService("MarketplaceService") local Players = game:GetService("Players") local gamePassID = 9291233 local player = Players.LocalPlayer local hasPass = false hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) if hasPass == true then --What to do when chatted and owned else MarketplaceService:PromptGamePassPurchase(player, gamePassID) end end)
And a RemoteEvent it Replicated Storage
Thanks to killerbrenden for the help!