Efficient way to loop/check?

  • What’s the most efficient way to check if a Player owns VIP every 10 seconds?
while wait(10) do
	CheckVIP()
	if CheckVIP then
		OwnedVIP()
	else
		print(Player.Name "Error")
	end
end
1 Like

The best way to do it imo is to not check every ten seconds and instead check when a player joins & have a button for them to click if they buy something. It’d save you a lot of network requests.

6 Likes

What if someone buys it, then they would have to leave the server and join back.

You’d be better off saving the fact that they have VIP as a BoolValue and using BoolValue.Changed rather than using an infinite loop.

2 Likes

That’s what the button is for. They’d click it and it’d force a recheck

2 Likes

Like this?

repeat
	wait()
	until Player.Character -- This is a client script so I had to use this instead of player added.
		CheckVIP()
	if CheckVIP then
		OwnedVIP()
	else
	print(Player.Name "Error")
end

or

game.Players.LocalPlayer.CharacterAdded:Wait()
		CheckVIP()
	if CheckVIP then
		OwnedVIP()
	else
	print(Player.Name "Error")
end

The best option would be to handle the purchase in game so that you know when they have completed purchasing a game pass. I think the next best option would be to check when they attempt to perform an action that requires special permissions. If they purchase it on the website and they may frequently perform actions that require game pass checks, it would be better to check at key transition points that (for FPS games after a round, after a death or checkpoint in an obby).

What type of game is this for? Maybe it can be integrated with the game features / loop.

1 Like

It’s for a Simulator Game.

What is it simulating and what is the VIP pass for? I hope you don’t mind, I’m just trying to come up with a specialized solution for your circumstances.

The VIP pass removes a door which gives you access to an area where more points spawn.

Ah, then I’d check when they touch the door with a debounce (so if they continue to touch the door the server wont spam checks). If you want the door to disappear permanently then I’d remove the door locally when they touch the door as mentioned above, when they join the game, purchase the VIP in game, or die/reset (if your simulator has that mechanic). It’d basically be the same function connected to all four of those events. Will this work in your case?

I think it will work. This is a Client sided script.

  • Below is the script, but without the variables.
local function CheckVIP()
	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
		PlayerVIP = true
	else
		PlayerVIP = false
	end
end

local function OwnedVIP()
	if PlayerVIP == true then
		local Door = game.Workspace:FindFirstChild("VIPDoor")
		if Door then
			Door:Destroy()
		end
	end
end

repeat 
	wait()
	until Player.Character --// game.Players.LocalPlayer.CharacterAdded:Wait()
		CheckVIP()
	if CheckVIP then
		OwnedVIP()
	else
	print(Player.Name "Error")
end

Here is an outline of what I was thinking:

local Players = game:GetService 'Players'
local MarketplaceService = game:GetService 'MarketplaceService'

local player = Players.LocalPlayer
local door = game.VIP_Door

local lastCheck = 0
local isVIP = false
local debounce = 5 -- seconds

local function handleVIP()
    if isVIP or tick() - lastCheck < debounce then
        return -- We've already removed the door or recently checked
    end

    lastCheck = tick()
    isVIP = MarketplaceService:UserOwnsGamePassAsync(
       player.UserId,
        GamepassID)

    if isVIP then
         door:Destroy()
    end
end

local function onDoorTouched(part)
    if player.Character and player.Character:IsAncestorOf(part) then
        handleVIP
    end
end

local function onPurchaseFinished(otherPlayer, pass, wasPurchased)
    if otherPlayer == player and pass == GamepassID and wasPurchased then
        handleVIP()
    end
end

handleVIP() -- Check when they join the game
player.CharacterAdded:Connect(handleVIP) -- Check when they reset
door.Touched:Connect(onDoorTouched) -- Check when they hit the door
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPurchaseFinished) -- Check when they buy the pass in game
4 Likes

Thanks for the help :smile:

1 Like