I wanted to know how I would prompt a game pass purchase from clicking on a part. However, MouseClick does not work in a local script.
local MPS = game:GetService("MarketplaceService")
local gamepassId = 12894476
local function promptPurchase()
local player = game.Players.LocalPlayer
local hasPass = false
local success, message = pcall(function()
hasPass = MPS:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if hasPass == true then
print("u already have")
else
MPS:PromptGamePassPurchase(player,gamepassId)
end
end
script.Parent.MouseClick:Connect(promptPurchase)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
local target = mouse.Target
if target == workspace.PART_PATH then
promptPurchase()
end
end)
edit : When the player clicks the clickDetector you should fire a remote event with whatever info you need and then handle the purcashe stuff on the server
Wait, is the LocalScript inside the ClickDetector? If so, it doesn’t work. The LocalScript must be inside the player, and then you find the path to the ClickDetector.
Yeah that was the problem. you cant use local scripts outside of a player’s character unless its in playerGui or StarterCharacterScript / StartPlayerScripts
As @IProgramForFun said, you should handle this in the server.
Just get the player from MouseClick event.
Try something like this:
local MPS = game:GetService("MarketplaceService")
local gamepassId = 12894476
local function promptPurchase(player)
local hasPass = false
local success, message = pcall(function()
hasPass = MPS:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if hasPass == true then
print("u already have")
else
MPS:PromptGamePassPurchase(player,gamepassId)
end
end
script.Parent.MouseClick:Connect(function(player)
promptPurchase(player)
end)