Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

loop checking if player has gamepass doesn't work?

I'm not sure if I put the loop on the wrong place or something, but I've tried everything and it just doesn't work... I don't have the gamepass and I went into the server side and changed my "XM" value to 2 which should be change back to 1, but it doesn't do that

wait(1)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
 
local gamePassID = 111
 
game.Players.PlayerAdded:Connect(function(player)
	while wait() do
	local hasPass = false
 
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
	if hasPass == true then
	player:WaitForChild("leaderstats")
  player:FindFirstChild("Stats").XM.Value = 2
end
	if hasPass == false then -- I've tried else and elseif but they don't work
	 player.Stats.XM.Value = 1
	end
end
end)

1 answer

✓ Accepted answer

You can just do this with an if statement:

local market = game:GetService("MarketPlaceService")

game.Players.PlayerAdded:Connect(function(player)
        wait(5) --letting the player to load in
        if market:UserOwnsGamePassAsync(player.UserId, 111) then
                -- the player has the gamepass
            else
                -- the player doesn't have the gamepass
        end
end)

Insert that code in a SCRIPT inside SERVERSCRIPTSERVICE.

Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.