Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to cast value to Object?

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

✓ Accepted answer
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!

This discussion is preserved as it was. New answers, comments, and votes are closed.