The idea with this code is that separate scripts in my game that use UserOwnsGamePassAsync() can just go based off the instance value found inside the player. Thoughts?
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Gamepasses = {
[19605766] = "x2_Stars";
[19605770] = "x5_Stars";
[19605777] = "x2_Crowns";
[19605781] = "x5_Crowns";
[20889326] = "Extra_Hats";
[21393466] = "Plus"
}
function PlayerOwnsGamepass(Player, ID)
while true do
local Success, Result = pcall(function()
return MarketplaceService:UserOwnsGamePassAsync(Player.UserId, ID)
end)
if Success then
return Result
else
warn(string.format([[
==================
ERROR CHECKING GAMEPASS
PLAYER NAME: %s
GAMEPASS ID: %s
ERROR: %s
==================
]]),Player.Name, ID, Result)
wait(3)
end
end
end
function PlayerAdded(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "Gamepasses"
for ID, Name in pairs(Gamepasses) do
local Value = Instance.new("BoolValue", Folder)
Value.Name = Name
Value.Value = PlayerOwnsGamepass(Player, ID)
end
while wait(60) do
for ID, Name in pairs(Gamepasses) do
local Value = Folder:FindFirstChild(Name)
if Value and not Value.Value then
Value.Value = PlayerOwnsGamepass(Player, ID)
end
end
end
end
Players.PlayerAdded:Connect(PlayerAdded)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, ID, WasPurchased)
if Gamepasses[ID] and Player:FindFirstChild("Gamepasses") and Player.Gamepasses:FindFirstChild(ID) and WasPurchased then
Player.Gamepasses[ID].Value = true
end
end)
Apart from what D0RYU mentioned I also suggest not using this
While wait() do format
Instead while true do wait()
Both are bad but while true do is better because
if the implementation of wait() changes then your game will break as the loop might never run.
I saw this on Sleitnick’s channel where he explains it nicely
At least try to make claims with more context, only don’t use the parent parameter of Instance.new() if you are going to be setting properties.
No, Roblox staff them selves stated that they won’t change the return value of wait() or task.wait() to a non truthy value, the formter is safe to use. @Zohair_028
Imagine if wait( ) stopped returning a value.
In that case your loop will never execute.
If wait() stopped returning a true value everything you wanted to happen will basically stop.
If you have many of them through out your game, it may be causing a problem in the future.
My explanation might not have been very good or clear so I suggest watching Sleitnick’s video here Roblox: Task Scheduler & Avoiding Wait - YouTube
where he explains it really well and also mentions other things.