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

Function doesn't check if PlayerOwnsAsset when touching a part?

Asked by 6 years ago

This code is supposed to check if the player owns a certain asset when the player steps on the part and I'm wondering why this won't work.

function OwnShades(plr)

    if game:GetService("MarketplaceService"):PlayerOwnsAsset(plr,30331986) then
        print(plr.Name .. " owns the Midnight Shades")
    else
        print(plr.Name .. " doesn't own the Midnight Shades")
    end
end

game.Workspace.Part.Touched:connect(OwnShades)

1 answer

Log in to vote
2
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Unlike the PlayerAdded event, which is the example on the wiki, the Touched event gives you the part that touched rather than the player. To get the player from the part that touched, you can use GetPlayerFromCharacter. So to do this, you would check if the part's parent was a character, and if they were, get the player that the character belongs to:

local function OwnsShades(Hit)
    local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if plr then
        if game:GetService("MarketplaceService"):PlayerOwnsAsset(plr,30331986) then
            print(plr.Name .. " owns the Midnight Shades")
        else
            print(plr.Name .. " doesn't own the shades")
        end
    end
end
workspace.Part.Touched:Connect(OwnsShades)

This example would be for a server script.

Remember that the character is the physical model in workspace (that will touch the brick), and the player is the object in the Players folder.

Also make sure that the part you want to detect touch for is called Part and in workspace (and is the only object with this name). You can always move the script inside the part and use script.Parent rather than workspace.Part if you wanted to.

Ad

Answer this question