Gamepass purchase not working

Hello, I am trying to fix an issue where the player buys a gamepass, and if purchased, a remote event is sent from the client to the server, however I get this error when trying to reference the player.

Client

local plr = Players.LocalPlayer

few lines later…

event:FireServer(gamePassID, plr)

Server

hasPass = gs:UserOwnsGamePassAsync(plr.UserId, id)

And I am getting the following error.

  13:20:05.268  Error while checking if player has pass: ServerScriptService.RobuxShopHandler:8: attempt to index number with 'UserId'  -  Server - RobuxShopHandler:12

I have not found a solution so far, so I am asking here.

Firing to the server automatically gives the player instance who fired the event as the first parameter, how does your Onserverevent look like?

Here is what it looks like.
event.OnServerEvent:Connect(function(id, plr)

local hasPass = false

local success, message = pcall(function()
	hasPass = gs:UserOwnsGamePassAsync(plr.UserId, id)
end)

if not success then
	warn("Error while checking if player has pass: " .. tostring(message))
	return
end

if hasPass then
	if id == 16234236 then --Rainbow Trail
		local trail = plr.Character.HumanoidRootPart:FindFirstChild("Trail")
		if trail then
			trail:Destroy()
		end
		local newtrail = game.ServerStorage.Trails.RainbowTrail:Clone()
		newtrail.Parent = plr.Character.HumanoidRootPart
		newtrail.Name = "Trail"
	end
	
else
	print("The user "..plr.Name.. " does not have the gamepass with the id "..id)
	-- Player does NOT own the game pass; prompt them to purchase
end

end)

Change that to

event.OnServerEvent:Connect(function(plr,id)

The player instance who fired it is always given first, you also don’t need to give the player yourself as it does that for you, so just fire the id only

event:FireServer(gamePassID)

First parameter of OnServerEvent is the player always.

Removing the player gave me this.

 Error while checking if player has pass: ServerScriptService.RobuxShopHandler:8: attempt to index nil with 'UserId

Did you also change the onServerEvent so plr is first and id is second? When I mentioned removing the player, I meant that only for the FireServer, as it’s unneeded, you still need plr for the first parameter of OnServerEvent, as that is given automatically

I’m an idiot lol, it seems like the gamepass is registered when the player leaves. Sorry for wasting your time.

1 Like