Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

"Unable to cast value to object" Shop gui not working?

Asked by
Time_URSS 146
5 years ago
Edited 5 years ago

Greetings,

I've been making a shop gui with some gamepasses in, and it should detect if it's a gamepass or not. The problem is that I get an error and I don't know how to solve it. Here are the scripts:

Leaderstats & Events script

local MarketPlaceService = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local HasGamepass

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"

    local Coins = Instance.new("IntValue",leaderstats)
    Coins.Name = "Coins"
    Coins.Value = 10000
end)

game.ReplicatedStorage.GetInfo.OnServerInvoke = function(player,name)
    return game.ServerStorage.ShopItems[name].Coins.Value,game.ServerStorage.ShopItems[name].IsGamepass.Value,game.ServerStorage.ShopItems[name].GamepassID.Value
end

game.ReplicatedStorage.CheckSale.OnServerInvoke = function(player,name,boolGamepass,gamepassId)
    local price = game.ServerStorage.ShopItems[name].Coins.Value

    if player.leaderstats.Coins.Value >= price then
        if boolGamepass == true then
            local Pass,Error = pcall(function()
                HasGamepass = MarketPlaceService:UserOwnsGamePassAsync(player.UserId,gamepassId)
            end)

            if not Pass then
                error("Failed while checking if player owned gamepass: "..Error)
            end

            if HasGamepass == false then
                print(player.UserId,gamepassId)

                MarketPlaceService:PromptGamePassPurchase(player.UserId,gamepassId)
            elseif HasGamepass == true then
                game.ServerStorage.ShopItems[name][name]:Clone().Parent = player.Backpack
                game.ServerStorage.ShopItems[name][name]:Clone().Parent = player.StarterGear
            end
        elseif boolGamepass == false then
            game.ServerStorage.ShopItems[name][name]:Clone().Parent = player.Backpack
        end

        return true
    else
        return false
    end
end

Select Gun Script

local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local ToolNameText = script.Parent.ToolName
local ToolNameDesc = script.Parent.Description
local Information = script.Parent.Parent.Parent.Parent.Information

ToolNameText.Text = tostring(ToolNameText.ToolName.Value)
ToolNameDesc.Text = "Description: " .. tostring(ToolNameDesc.Description.Value)

script.Parent.MouseButton1Click:Connect(function(player)
    local Price,BoolGamepass,GamepassID = ReplicatedStorage.GetInfo:InvokeServer(tostring(ToolNameText.ToolName.Value))

    if Price ~= nil then
        Information.ToolName.Text = tostring(ToolNameText.ToolName.Value)
        if BoolGamepass == true then
            Information.ToolPrice.Text = "Gamepass: " .. Price .. " R$"
            ReplicatedStorage.BuyProcess:Fire(ToolNameText.ToolName.Value,Price,BoolGamepass,GamepassID)
        elseif BoolGamepass == false then
            Information.ToolPrice.Text = tonumber(Price)
            ReplicatedStorage.BuyProcess:Fire(ToolNameText.ToolName.Value,Price,BoolGamepass)
        end
    end
end)

Buy with Rubles Script

local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Information = script.Parent.Parent
local ToolNameText = Information.ToolName.ProductName
local ToolNameDesc = Information.ToolPrice.ProductPrice

ReplicatedStorage.BuyProcess.Event:Connect(function(name,price)
    repeat wait(0.1) until script.Parent.MouseButton1Click
    script.Parent.MouseButton1Click:Connect(function(player)
        local Success = ReplicatedStorage.CheckSale:InvokeServer(name,nil,nil)

        if Success == true then
            print("Purchase success!")
        elseif Success == false then
            print("Not enough money!")
        end
    end)
end)

Buy with Robux script

local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Information = script.Parent.Parent
local ToolNameText = Information.ToolName.ProductName
local ToolNameDesc = Information.ToolPrice.ProductPrice
local player = game.Players.LocalPlayer
local HasGamepass = false 

ReplicatedStorage.BuyProcess.Event:Connect(function(toolName,price,boolGamepass,gamepassId)
    repeat wait(0.1) until script.Parent.MouseButton1Click
    script.Parent.MouseButton1Click:Connect(function()
        if gamepassId == nil then
            local Success = ReplicatedStorage.CheckSale:InvokeServer(toolName)

            if Success == true then
                player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price

                ServerStorage.ShopItems[toolName][toolName]:Clone().Parent = player.Backpack
            elseif Success == false then
                print("Not enough money!")
            end
        elseif gamepassId ~= nil then
            print(player.UserId.." - "..gamepassId)

            local Pass,Error = pcall(function()
                HasGamepass = MarketPlaceService:UserOwnsGamePassAsync(player.UserId,gamepassId)
            end)

            if not Pass then
                error("Failed while checking if player owned gamepass: "..Error)
            end

            print(toolName,boolGamepass,gamepassId)

            local Success = ReplicatedStorage.CheckSale:InvokeServer(toolName,boolGamepass,gamepassId)

            if Success == true then
                print("Purchase success!")
            elseif Success == false then
                print("Not enough money!")
            end
        end
    end)
end)

If you need the hierarchy of the gui and everything, ask me in the comments.

0
What line and what script is the error happening in? Try to post code only relevant to the problem. pidgey 548 — 5y
0
It errors in line 34, in the leaderstats script. Time_URSS 146 — 5y
0
PromptGamePassPurchase ( Instance player , int64 gamePassId ) - your first argument at line 34 is a number (player.UserId). This method requires a Player object, not a number. pidgey 548 — 5y
0
MarketPlaceService:PromptGamePassPurchase(player, gamepassId) pidgey 548 — 5y
View all comments (2 more)
0
I see Time_URSS 146 — 5y
0
Now the error is "Unable to cast Instance to int64" Time_URSS 146 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

MarketplaceService:PromptGamePassPurchase, this function takes Player Object as an argument and GamepassId, which means, that you should not pass UserId, instead you should just pass Player. like that

MarketPlaceService:PromptGamePassPurchase(player,gamepassId)
0
Thank ou Time_URSS 146 — 5y
0
Wait, now I receive this error: Unable to cast Instance to int64 Time_URSS 146 — 5y
Ad

Answer this question