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?

Asked by
lytew 99
4 years ago

I created this script to make that roblox purchase screen appear for those who do not have a GamePass and DO NOT appear for those who have it. The problem is that this error is showing "Unable to cast value to Object, on line 21" and the screen sells does not appear.

local Gamepass1 = workspace.LEVEL2_MAP.GamePasstouch


local Id1 = 2544515


local debounce = false
Gamepass1.Touched:Connect(function(t)
    if t.Parent:FindFirstChild('Humanoid') then
    if debounce == false then
        debounce = true
    local haspass = false
    local name = t.Parent.Name
    local b = game.Players:FindFirstChild(name)
        local success, message = pcall(function()
        haspass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(b.UserId, Id1)
    end)

    if haspass == false then
        print('stop')
            game:GetService("MarketplaceService"):PromptGamePassPurchase(b.UserId, Id1)

    elseif haspass == true then 
        print("go")
    end

    wait(0.5)
    debounce = false
    end
    end
end)


1 answer

Log in to vote
1
Answered by
ImTrev 344 Moderation Voter
4 years ago
Edited 4 years ago
local Gamepass1 = workspace.LEVEL2_MAP.GamePasstouch
local Id1 = 2544515

local debounce = false
Gamepass1.Touched:Connect(function(t)
    if t.Parent:FindFirstChild('Humanoid') then
        if not debounce then
            debounce = true
        local haspass = false
        local name = t.Parent
        local b = game.Players:GetPlayerFromCharacter(name)
        local success, message = pcall(function()
            haspass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(b, Id1)
        end)
        if not haspass then
            print('stop')
            game:GetService("MarketplaceService"):PromptGamePassPurchase(b, Id1)
        else 
            print("go")
        end

        wait(0.5)
        debounce = false
        end
    end
end)

I found your error, when using "PromptGamePassPurchase", you have to pass the player as the first argument, not the UserId!

Ad

Answer this question