✓ Accepted answer
This is a common mistake. May I educate you on server scripts and local scripts.
The server has no concept of "local player".
Simply, Players.LocalPlayer is nil to the server.
Ancestry
The top ancestor (excluding the DataModel and the Players service) of a LocalScript is typically the LocalPlayer. You can place them in ReplicatedFirst and have them execute there but we aren't going to talk about that. And LocalScripts run for one client, you. Scripts run on the server, a poweful computer that is located in the Roblox HQ.
Why LocalPlayer is nil on the server
Think of it as your school classmates and your teacher. Think of your school classmates as other clients, and think of your teacher as the server. If you told your teacher to "get the local student," your teacher would be very confused, because which student is the local student? For this reason, Players.LocalPlayer is nil on the server. Your game could have multiple players in it, and when you try using Players.LocalPlayer, it can't just randomly pick a player. It makes no sense.
Not a mind reader
Code cannot read your mind. This is why it must be all typed out. So instead of doing this:
make a part inside of the workspace with color red
You must do this:
local part = Instance.new("Part");
part.BrickColor = BrickColor.new("Really red");
part.Parent = game:GetService("Workspace");
Applying this knowledge, and more
When you listen for the ClickDetector.MouseClick event, the listener gets the player who clicked as an argument. So use that.
Also, MP and id are constants, so why declare them each time the button is clicked? One declaration will suffice.
local MarketplaceService = game:GetService("MarketplaceService");
local PRODUCT_ID = 562717431;
script.Parent.ClickDetector.MouseClick:Connect(function(client)
print("hi");
MarketplaceService:PromptProductPurchase(client, PRODUCT_ID);
end);
1 comment
You need to wait it out. plr = game.Players:WaitForChild('LocalPlayer') !