I’m attempting to connect a gamepass to a click function. The idea is that when the function is triggered, the function will check for a gamepass and if the player has it, it will print “Works!”. I’m not quite sure how to get it to work, some help would be appreciated. Thanks.
local MarketplaceService = game:GetService("MarketplaceService")
function phc1()
local function phcpassownercheck()
if MarketplaceService:UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, 8080398) then
print("Works!")
end
end
end
ButtonPHC1.MouseButton1Click:Connect(phc1)
The problem is you have a nested function inside phc1’s body.
You never called that function, thus making it seem like it doesn’t work.
Remove the outer function while keeping the inner one.
heck, just pass a function literal since this code isn’t really reusable elsewhere.
ButtonPHC1.Activated:Connect(function()
if MarketplaceService:UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, 8080398) then
print("Works!")
end
end)
Notice my use of Activated here. This is the preferred way of listening for UI button input.
I honestly have no idea what’s going on. I did some testing in Studio, and the only error message I found was: UserID is not a valid member of Player.
And the code I was testing with was:
Code
wait(5)
local MarketplaceService = game:GetService("MarketplaceService")
function phc1()
local function phcpassownercheck()
if MarketplaceService:UserOwnsGamePassAsync(game.Players.Secretum_Flamma.UserId, 8080398) then
print("Works!")
else
print("NO GAMEPASS")
end
end
end
script.Parent.MouseClick:Connect(phc1)
-- Was testing to see the status of the gamepass. Nothing printed.
while true do
wait(0.1)
print(MarketplaceService:UserOwnsGamePassAsync(game.Players.Secreum_Flamma.UserId, 8080398))
end