Dev product touched event

I’m trying to do dev products with touched event, if I local test it with 2 players and Player 1 touches the part this shows on Players 2 output: MarketplaceService:PromptProductPurchase called from a local script, but not called on a local player. Local scripts can only prompt the local player. Is this normal or did I something wrong?

This is the local script:

local mps = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local Wins15 = game.Workspace.Part

Wins15.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		local plrs = Players:GetPlayerFromCharacter(hit.Parent)
		mps:PromptProductPurchase(plrs, 1528483289) 
		end
end)
1 Like

Check if the Child “Part” in workspace is just 1 child, in other words. there might be more than child named with “Part” in the workspace,

possible solution:
Rename the child to “Wins15”

after you change the name of the part, change the variable Wins15 to this:

local Wins15 = game.Workspace.Wins15 
2 Likes

Its still the same outcome, output still says: MarketplaceService:PromptProductPurchase called from a local script, but not called on a local player. Local scripts can only prompt the local player.

1 Like

Check if the touching part/character is Player2’s character, like this :

local mps = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local Wins15 = game.Workspace.Part

Wins15.Touched:Connect(function(hit)
	if not hit or not hit.Parent:FindFirstChild("Humanoid") then return end
	local plrs = Players:GetPlayerFromCharacter(hit.Parent)
	if plrs ~= Players.LocalPlayer then return end
	mps:PromptProductPurchase(plrs, 1528483289) 
end)
1 Like

returning will stop the script, maybe he should not use the return at all. just use not or == in if statements

1 Like
local MPS = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Wins15 = game.Workspace.Part

Wins15.Touched:Connect(function(hit)
	if hit.Parent and hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then

	local plr = Players:GetPlayerFromCharacter(hit.Parent)
		if Players.LocalPlayer == plr then
			MPS:PromptProductPurchase(plr, 1528483289) 
		end
	end
end)
2 Likes

both works the same, but if he wants it with no return then this is the solution

Returning is most likely used in returning values from functions. if not then it is used to stop a script, like:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
	if plr.UserId ~= 1 then return end
	--code
end)

this is more like a good use for the script endings return.

What he’s using is a guard clause, it helps in readability and the return is intended to stop the script if per say, hit is missing.

You also avoid nesting ends resulting in less indents.

While your code will work, and is same in function. It just looks nicer to read and prevents you indenting too much.

hit will never be = nil if the event fires btw.

also actually the more ends are there the easier it is to manipulate the scripts, usually i type 200 lines per day. so i use lots of ends because i use lots of If statements or loops or functions, it helps everything to look easier and understand unknown scripts if you’re working in a commission or smth.

I know, however guard clauses is a technique that can be used in many scenarios. say if you forgot to send parameters for a function as in below.

function FunctionThatPrints(whatToPrint)
	-- Stops the function from running unless "whatToPrint" is passed to the function
	if not whatToPrint then return end

	-- This will not run, as the check above stops it from doing so.
	print(`coolswag {whatToPrint}`)
end

However, if you prefer you can still write is as

function FunctionThatPrints(whatToPrint)
	if whatToPrint then
		print(`coolswag {whatToPrint})
	end
end

Also this code would just stop and not return anything if userId == 1, as you are effectively just using a guard clause.

Maybe you should address the LocalPlayer itself instead of addressing all of the players in the variable

local Players = game:GetService("Players")

Maybe you should try

local Players = game:GetService("Players").LocalPlayer

if this is a local script, this should work fine. The OP’s script looks fine in my opinion.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.