Unable to cast Instance to int64?

hey all. Iv’e been having some issues with gamepasses and i everytime i try to check if someone owns a gamepass, i aways get this error: “Unable to cast Instance to int64” at Line 17. This issue has been bothering me since 2018.
Here’s the script:

gpid = 2725107 --Game Pass ID
tools = {"Handgun", "Shovel", "Health Kit"} --Gamepass Tools




GPS = game:GetService("MarketplaceService")
function respawned(char)
	local player = game.Players:FindFirstChild(char.Name)
	--print("Respawned")
	if char:FindFirstChild("Head") ~= nil then
		--print("Player Found")
		if GPS:UserOwnsGamePassAsync(player, gpid) then
			--print("Has GPID")
			for i = 1,#tools do
				game.Lighting:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
			end
		else
			--print("No GPID")
		end
	end
end
game.Workspace.ChildAdded:connect(respawned)
1 Like

UserOwnsGamePassAsync uses Player.UserId

6 Likes

Several points made here:

  1. Store the objects in ServerStorage
  2. As previously mentioned, UserId and not player object.
  3. Consider using the canonical events below here, all functions are anonymous here.
  4. Optionally, set all gamepass objects in a folder with the same name of the gamepass ID and use :GetChildren(), clone and add to player’s backpack.

Example
Fixed version

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage") -- store things in ServerStorage, not Lighting

local gpid = 2725107 --Game Pass ID
local tools = {"Handgun", "Shovel", "Health Kit"} --Gamepass Tools

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gpid) then
			for i = 1,#tools do
				ServerStorage:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
			end
		end
	end)
end)
8 Likes

Ohh, i see what iv’e done wrong. I completely forgot about (player.UserId, gpid). Also i was just temporary storing items in lighting. And thanks. Helped alot.