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

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

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

✓ Accepted answer

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.

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