Around thanksgiving I made a code to give people their items but it’s giving me an Unable to cast Instance to int64 what should I do?
local MarcketplaeService = game:GetService("MarketplaceService")
local Shop = script.Parent.Shop
local Gravityblock = Shop.Gravityblock
local player = game.Players.LocalPlayer
local GravityID = 25344998
local success, message = pcall(function()
hasPass = MarcketplaeService:UserOwnsGamePassAsync(player.UserId, Gravityblock)
end)
if hasPass then
print("player already has pass")
else
MarcketplaeService:PromptGamePassPurchase(player, Gravityblock)
end
I’m assuming line 15 is this line, as you never mentioned what line the error is actually on.
PromptGamePassPurchase takes two arguments, the player and the gamePassId. Unable to cast Instance to int64 means you’re trying to pass an instance in place of where the gamePassId should be.
As you can see, Gravityblock
is an instance and not the ID. I think you mean to use GravityID
MarketplaeService:PromptGamePassPurchase(player, GravityID)
EDIT: I just noticed, you’re also spelling market wrong.
2 Likes
As @amadeupworld2 mentioned, the second parameter of the UserOwnsGamePassAsync
method expects the ID of a gamepass (a number) to be passed to it. However, the “Gravityblock” variable which was input is referencing an Instance.
It seems like the “GravityID” variable (which is a number) is what should have been input in place of “Gravityblock”:
hasPass = MarcketplaeService:UserOwnsGamePassAsync(player.UserId, GravityID)
1 Like
the line that has the error is line 15 so yeah.
There are no more errors but when i test it it does not give any thing.
the other script should have cloned the item.
Since you didn’t include the other script/any details about it in your post, I’m not sure if and or how you intend to communicate the result from this codeblock with the other script or not.
The code in the original post only prints “player already has pass” when the script determines that the player owns the gamepass; if the other script needs to know whether or not the player owns the gamepass before continuing, that could be added where the print statement is within the “if hasPass then” conditional statements.
local MarketplaeService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local GravityblockID = 25344998
game.Players.PlayerAdded:Connect(function(player)
local success, message = pcall(function()
**hasPass = MarketplaeService:UserOwnsGamePassAsync(player.UserId, GravityblockID)**
end)
if hasPass then
print("player already has pass")
local Handle = game.ServerStorage.Gravity:Clone()
Handle.Parent = player.Backpack
end
end)
local function onPromptGamepasssPurchaseFinished(player, purchasedpassID, purchaseSuccess)
if purchaseSuccess == true and purchasedpassID == GravityblockID then
print(player.Name.."pass has been purchased")
local Handle = game.ServerStorage.Gravity:Clone()
Handle.Parent = player.Backpack
end
end
MarketplaeService.PromptGamePassPurchaseFinished:Connect(function(player, purchasedpassID, purchaseSuccess)
end)
has a W003 and If I put local Infront if hasPass will become a W001.
The error is occuring because the “hasPass” variable was created and initialized (first assigned a value) inside of the pcall, meaning that it can only be accessed in that scope.
Because the conditional statement that checks if hasPass
returned a value is in a different scope, it essentially doesn’t know that a variable called “hasPass” was created and assigned a value.
Since you’re already using a pcall, I’d recommend returning the value of the UserOwnsGamePassAsync
call to resolve this:
-- "hasPass" will refer to the value returned from the function if it was successful
local success, hasPass = pcall(function()
return MarketplaeService:UserOwnsGamePassAsync(player.UserId, GravityblockID)
end)
if success and hasPass then
-- Continue
Currently, the player is only provided the item if they own the gamepass upon joining (from the PlayerAdded
event connected to an anonymous function) or if they purchase the gamepass while in-game.
Just in case you intended for the player to keep the gamepass-related item upon respawning, I’d suggest making use of the CharacterAdded
event to detect when a player’s Character respawns and would need to be granted the item again.
Anddd one more suggestion, to abide by the D.R.Y (Don’t Repeat Yourself) principle, I would recommend creating a separate function which provides the item to the player so that you can activate an individual function whenever they need it:
Example:
local MarketplaeService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local GravityblockID = 25344998
local function giveItem(player) -- This could be expanded upon to provide different tools without needing to reference every individual one but this is a basic example to start
local gravityTool = ServerStorage.Gravity
local clonedTool = gravityTool:Clone()
local Backpack = player:WaitForChild("Backpack")
clonedTool.Parent = Backpack
end
-- Example from the function activated from the PlayerAdded event...
if hasPass then
print("player already has pass")
giveItem(player)
end
-- Example of the function activated from PromptGamePassPurchaseFinished event...
local function onPromptGamepasssPurchaseFinished(player, purchasedpassID, purchaseSuccess)
if purchaseSuccess == true and purchasedpassID == GravityblockID then
giveItem(player)
end
end
MarketplaeService.PromptGamePassPurchaseFinished:Connect(onPromptGamepasssPurchaseFinished)