Prompt t-shirt purchase

I want to prompt a player to buy my t-shirt when the player touches a part, but it doesn’t seem to be working.

This is my code:

local SHIRT_ID = 8896035930

script.Parent.Touched:Connect(function(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChild("Humanoid")
	if humanoid then
		game:GetService("MarketplaceService"):PromptPurchase(game.Players.LocalPlayer, SHIRT_ID)
	end
end)

Screen Shot 2022-02-22 at 9.39.40 AM

I don’t get any error messages. Help would be appreciated.

Maybe try this instead.

local SHIRT_ID = 8896035930

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChildOfClass("Humanoid") then
		game:GetService("MarketplaceService"):PromptPurchase(game.Players:GetPlayerFromCharacter(Hit.Parent), SHIRT_ID)
	end
end)

You can’t call Local Player from a Server script. You need to change the script to a local script.

Create a local script and paste the same code you have inside. Then delete the server script you originally have.

It will not work, local script can’t use touched function.

LocalScripts CAN use the touched function, but they can not be inside of workspace or any other non-player related areas, meaning that it has to be under something related to the player and run from there.

You need to use Touchinterest instead then. But it has to be in a local script because you can’t call local player in a server script no matter what you do.

Ok no problem, that means both scripts works but script is the most precise.

I changed the code for you. Make sure its in a local script.

local SHIRT_ID = 8896035930

script.Parent.Touched:Connect(function(player)
	game:GetService("MarketplaceService"):PromptPurchase(player, SHIRT_ID)
end)

LocalScripts don’t work in workspace, they need to be a descendant of a Player or Character, you need to use a normal script, @Misinformater’s code should work.

This is true, but you can get a Player if you have the right info, like their Character. You can use game.Players:GetPlayerFromCharacter() or even just get the Player with the Characters name by going through game.Players[Character.Name].

This would only work if script.Parent is part of the player’s character, which is obviously not what OP wants.

Thanks for info. I didn’t know that.