Is there a way for the script to check whether you own the gamepass or not so you don’t have to rejoin in order to receive the pet? If not that’s fine. Sorry if I’m asking so much lol.
I would suggest trying to bind the function to .PromptGamePassPurchase function to make it work without the need of rejoining.
Try to place this script in eg StarterPlayerScripts. This has to be done to make sure that the ownership caches after received event.
-- Basic data and services
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Product = 8530571
local MarketplaceService = game:GetService("MarketplaceService")
-- Initial check
local HasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Product)
local AwardPet = function (Character)
-- PLACE YOUR AWARD FUNCTION HERE
end
-- Bind an event which is fired when gamepass is bought in-game
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Purchaser, GamePassd, Purchased)
if not Purchased then return end -- Do not change when not purchased
if GamePassId ~= Product then return end -- Do not change when event is fired for different gamepass
if Purchaser == Player then -- Make sure this player bought the pass
HasPass = true
return AwardPet(Player.Character) -- Since he/she bought the pass, don't make him need to respawn.
end
end)
Player.CharacterAdded:Connect(function(Character)
if HasPass then
return AwardPet(Character) -- Award pet when character spawns and if has pass
end
end)
if Player.Character and HasPass then AwardPet(Player.Character) end -- Sometimes (especially in play solo mode) character loads faster than the event is bound. This fixes that.
I would advice you to check out more information about events as they are essential in scripting. If the answer was accurate and helpful, I hope you can resolve the topic with this post.
Previously proposed solutions won’t work with in-game purchase as nobody mentioned the PromptGamePassPurchaseFinished event which doesn’t affect the check function.
Good luck with further development!
Thank you, I will look into events. I’ll just leave the other script for now but once I have some more experience with events I’ll make sure to try and replace it! Thank you all for helping. @SansariGames @vitorpvp1337 @muffin1234 @rokec123 !