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

What is causing the MarketplaceService to say it's "unable to cast value to object"?

Asked by 5 years ago
local Lighting = game:GetService("Lighting")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local badgeId = "2124428647"
local toolNames = {"GhostBurger"}
local toolsParent = Lighting

local function onPlayerAdded(player)
    local function onCharacterAdded(character)
        if MarketplaceService:PlayerOwnsAsset(player.UserId, badgeId) then
            for i = 1, #toolNames do
                local tool = toolsParent:FindFirstChild(toolNames[i])
                if tool then
                    local clone = tool:Clone()
                    clone.Parent = player.Backpack
                end
            end
        end
    end

    if player.Character then
        onCharacterAdded(player.Character)
    end
    player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

It is line 11, where it lists the player.UserId, badgeId.

1 answer

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago

First of all:

PlayerOwnsAsset needs a player object, put you're passing string instead which causes the error.

Second of all:

The badge id must be an int, not a string:

local badgeId = 2124428647

Lastly:

UserHasBadgeAsync of BadgeService is the appropriate function to use, not PlayerOwnsAsset.

if game:GetService('BadgeService'):UserHasBadgeAsync(player.UserId, badgeId) then
0
Thank you Zombie_Panic 13 — 5y
Ad

Answer this question