Prompt GamePass From A Click Detector

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)
1 Like

Any form of user input only works in Local scripts

1 Like

So how would I detect a click in a local script?

1 Like

That depends on what you are wanting the player to click

1 Like

A part with a click detector. adfhgfj

1 Like

What if you use mouse.Target?

1 Like

Then it would prompt a purchase if i hover over it i think

1 Like

Any errors in the output perchance?
image
@elvinfcb9

Nope. it doesnt detect my click so it doesn’t run the function

1 Like

You could use ClickDetector.MouseClick and connect it to a function which would prompt the gamepass.

1 Like

Thats what I did, MouseClick doesnt work on local scripts i believe

How is it not detecting your click are you too far away?

If the click detector is in workspace there is no reason why it shouldnt be working unless your too far away

Nop, you can do something like this:

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)

Although I think ClickDetector works on client.

It does work on the client
image

1 Like

MouseClick should work on local scripts. Is there any errors in the output?

Why are you handling purchases on the client?

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.

Is the LocalScript inside the ClickDetector?

2 Likes

yea, ima try this is a server script and sees if it works.

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)

(Server Script)