Gamepass checking error

Hello everyone,

So I’m trying to make a car spawning system where some cars are free and some are locked behind a gamepass.

The problem that I’m having is that whenever a player who does not have a gamepass tries to spawn the locked car it does not prompt the gamepass.

It always gives out an error in the output and I can’t seem it figure it out.

Error: Error checking gamepass for player gersxon - Server - CarGamepassChecker:14

This is one of the scripts located in serverscriptservice:

local GamePassID = 655695661

local function checkGamepass(player)
	local success, hasGamepass = pcall(function()
		return player:HasGamePass(GamePassID)
	end)
	if success then
		return hasGamepass
	else
		warn("Error checking gamepass for player " .. player.Name)
		return false
	end
end

game.ReplicatedStorage.GamepassCheckEvent.OnServerInvoke = function(player, carName)
	print("Server: Checking if player " .. player.Name .. " has gamepass for car: " .. carName)
	local hasGamepass = checkGamepass(player)
	return hasGamepass
end

This one is a localscript in starterplayerscripts:

local GamePassID = 655695661


local cars = {
	{name = "Supercar (yellow)", locked = false}, 
	{name = "Supercar (green)", locked = true},  
	{name = "Supercar (blue)", locked = false},   
	{name = "Police Car", locked = true},         
	{name = "Dune Buggy (beige)", locked = true},
}

local player = game.Players.LocalPlayer
local screenGui = player.PlayerGui:WaitForChild("CarSpawner") 
local carSpawnerFrame = screenGui:WaitForChild("Spawner")     
local scrollingFrame = carSpawnerFrame:WaitForChild("CarsFrame") 
local carModelsFolder = game.ReplicatedStorage:WaitForChild("CarModels") 
local gamepassCheckEvent = game.ReplicatedStorage:WaitForChild("GamepassCheckEvent") 

local function onCarButtonClick(carName, locked)
	print("Car button clicked:", carName)
	local hasGamepass = gamepassCheckEvent:InvokeServer(carName)
	local carData = nil
	for _, car in ipairs(cars) do
		if car.name == carName then
			carData = car
			break
		end
	end

	if carData then
		local button = scrollingFrame:FindFirstChild(carName)
		if button then
			if carData.locked and not hasGamepass then
				button.Text = carName .. " (Locked)"
				game:GetService("MarketplaceService"):PromptPurchase(player, GamePassID)
			elseif not carData.locked or (carData.locked and hasGamepass) then
				button.Text = carName 
				local carModel = carModelsFolder:FindFirstChild(carName)
				if carModel then
					local carClone = carModel:Clone()
					carClone.Parent = workspace
					carClone:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame)
					print(carName .. " spawned for player " .. player.Name)
				else
					warn("Car model not found: " .. carName)
				end
			end
		else
			warn("Button for car not found: " .. carName)
		end
	end
end

local function bindCarButtons()
	for _, car in ipairs(cars) do
		local button = scrollingFrame:FindFirstChild(car.name)
		if button then
			if car.locked then
				button.Text = car.name .. " (Locked)" 
			else
				button.Text = car.name  
			end
			button.MouseButton1Click:Connect(function()
				onCarButtonClick(car.name, car.locked)
			end)
		else
			warn("Button for car not found: " .. car.name)
		end
	end
end

bindCarButtons()
1 Like

In you server script you are using player:HasGamePass() which is not a valid function of player. The function is instead located in the MarketPlaceService and you should use the function game:GetService("MarketplaceService"):UserOwnsGamePassAsync(GamePassId)

2 Likes

I honestly forgot about MarketplaceService for whatever reason, but thank you!