Instance Based Game Pass System

Hello, developers!

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)

use task.wait() instead of wait()
and don’t use the parent parameter when using Instance.new()

1 Like

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

Whats the difference between task.wait() and wait()? Does it make much of a difference?

Will definitely give the instance thing a go.

1 Like

Can you define this:

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

3 Likes

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.

1 Like

as you are right about this, you should still avoid doing it to get into a good habit

and yes I’m sorry for not adding more context, my bad

this won’t happen, roblox has said this many times

2 Likes

wait() is deprecated(or soon to be deprecated), so you should either use task.wait() or another way

Why would wait() be depreciated? It’s one of the most commonly used functions?? That just doesn’t make sense to me.

Why would wait() stop returning a value? Roblox would never do this as it would break several major games.

you don’t get it
they are deprecating wait(), BUT they are not removing it from the global functions

What is the point of depreciating wait()?

the task library is replacing it
spawn() and delay() are also being deprecated because of this library

Why?

I recommend you read through this