InternalServerError occuring when checking if player owns gamepass in a loop

So I have a script that fires events to unlock certain items in my game based on if the player is at a certain level, has a certain badge, or owns a gamepass. When the code is ran, it throws “InternalServerError” on the “hasGamepass()” function.

Here is my code

--//Services
local BadgeService = game:GetService("BadgeService")
local MarketPlaceService = game:GetService("MarketplaceService")

--//Objects
local UNLOCKABLE_CLASS = require(game.ServerStorage.Objects.UnlockableObject)

--//Misc info
local player = script:FindFirstAncestorWhichIsA("Player")
local level = game.Players[player.Name]:WaitForChild("leaderstats"):WaitForChild("Level")

local events = game.ServerStorage.Events.Bindables.Customization

local saberTypeUnlock = events.UnlockSaberType
local colorUnlock = events.UnlockColor
local hiltUnlock = events.UnlockHilt

--//Arrays
local saberTypes = {
	UNLOCKABLE_CLASS.new(2,2124932488),
}

local saberHilts = {
	--level
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 5,[2] = 1,[3] = 5,[4] = 5},["Protosaber"] = {[1] = 5,[2] = 1,[3] = 5,[4] = 5}},nil,15),--Dooku
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 6,[2] = 1,[3] = 6,[4] = 6},["Protosaber"] = {[1] = 6,[2] = 1,[3] = 6,[4] = 6}},nil,10),--Sword
	--gamepass
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 4,[2] = 4,[3] = 4,[4] = 4},["Protosaber"] = {[1] = 4,[2] = 4,[3] = 4,[4] = 4}},nil,nil,28692491),--Kylo
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 11,[2] = 1,[3] = 11,[4] = 11},["Protosaber"] = {[1] = 11,[2] = 1,[3] = 11,[4] = 11}},nil,nil,28693451),--lego curved
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 12,[2] = 1,[3] = 10,[4] = 10},["Protosaber"] = {[1] = 12,[2] = 1,[3] = 10,[4] = 10}},nil,nil,28693451),--Kylo lego
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 8,[2] = 1,[3] = 8,[4] = 8},["Protosaber"] = {[1] = 8,[2] = 1,[3] = 8,[4] = 8}},nil,nil,28693406),-- normal lego
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 9,[2] = 1,[3] = 9,[4] = 9},["Protosaber"] = {[1] = 9,[2] = 1,[3] = 9,[4] = 9}},nil,nil,28693406),--gold lego
	UNLOCKABLE_CLASS.new({["Lightsaber"] = {[1] = 10,[2] = 1,[3] = 10,[4] = 10},["Protosaber"] = {[1] = 10,[2] = 1,[3] = 10,[4] = 10}},nil,nil,28693406),--black lego
}

local colors = {
	UNLOCKABLE_CLASS.new(12,2124932500),-- Ancient orange
	UNLOCKABLE_CLASS.new(13,2124932570),-- Ancient White
	UNLOCKABLE_CLASS.new(14,2124932572),-- DC red
	UNLOCKABLE_CLASS.new(15,2124932576),-- DC blue
	UNLOCKABLE_CLASS.new(16,2124932581),-- DC Ice blue
	UNLOCKABLE_CLASS.new(17,2124932583),-- dc teal
	UNLOCKABLE_CLASS.new(18,2124932588),-- dc green
	UNLOCKABLE_CLASS.new(19,2124932592),-- dc lime
	UNLOCKABLE_CLASS.new(20,2124932595),-- dc yellow
	UNLOCKABLE_CLASS.new(21,2124932598),-- dc orange
	UNLOCKABLE_CLASS.new(22,2124932601),-- dc purple
	UNLOCKABLE_CLASS.new(23,2124932603),-- dc pink
	UNLOCKABLE_CLASS.new(24,2124932604),-- dc white
}

local function userOwnsBadge(badgeID)
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
	end)

	-- If there's an error, issue a warning and exit the function
	if not success then
		warn("Error while checking if player has badge!")
		return
	end
	if hasBadge then
		return true
	end
end

local function hasGamepass(id)
	return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, id)
end

local function checkLevel(requirement)
	if not requirement then return end
	if level.Value >= requirement then
		return true
	end
end

local function checkBadge(id)
	if not id then return end
	if userOwnsBadge(id) then
		return true
	end
end

local function checkGamepass(id)
	if not id then return end
	if hasGamepass(id) then
		return true
	end
end

level.Changed:Connect(function(v)
	for i,v in pairs(colors) do
		if checkLevel(v:getLevel()) then
			colorUnlock:Fire(player,v:getIndex())
		end
	end

	for i,v in pairs(saberTypes) do
		if checkLevel(v:getLevel()) then
			saberTypeUnlock:Fire(player,v:getIndex())
		end
	end

	for i,v in pairs(saberHilts) do
		if checkLevel(v:getLevel()) then
			hiltUnlock:Fire(player,v:getIndex())
		end
	end
end)

local function setup()
	for i,v in pairs(colors) do
		if checkLevel(v:getLevel()) then
			colorUnlock:Fire(player,v:getIndex())

		elseif checkBadge(v:getBadgeId()) then
			colorUnlock:Fire(player,v:getIndex())

		elseif checkGamepass(v:getGamepassId()) then
			colorUnlock:Fire(player,v:getIndex())
		end
	end
	
	for i,v in pairs(saberTypes) do
		if checkLevel(v:getLevel()) then
			saberTypeUnlock:Fire(player,v:getIndex())

		elseif checkBadge(v:getBadgeId()) then
			saberTypeUnlock:Fire(player,v:getIndex())

		elseif checkGamepass(v:getGamepassId()) then
			saberTypeUnlock:Fire(player,v:getIndex())
		end
	end
	
	for i,v in pairs(saberHilts) do
		if checkLevel(v:getLevel()) then
			hiltUnlock:Fire(player,v:getIndex())

		elseif checkBadge(v:getBadgeId()) then
			hiltUnlock:Fire(player,v:getIndex())

		elseif checkGamepass(v:getGamepassId()) then
			hiltUnlock:Fire(player,v:getIndex())
		end
	end
end

setup()

You should probably wrap it inside a pcall to handle any raised/thrown errors.

local function hasGamepass(id)
	local success, result = pcall(function()
		return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, id)
	end)
	
	if success then
		if result then
			return true
		else
			return false
		end
	else
		return false
	end
end
1 Like