Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to prompt purchase of a dev product when a brick gets touched?

Hello, here is the script I have so far and im getting this error:

15:48:37.186 - Player is not a valid member of DataModel

Can anybody help me?

Here is my script:

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent and game.Players:FindFirstChild(hit.Parent.Name) then
		game:GetService("MarketplaceService"):PromptProductPurchase(game.Player:FindFirstChild(hit.Parent.Name), 408797380)
	end
end)

1 comment

0

game.Players, ah. Also don't use FindFirstChild on Players, just use GetPlayerFromCharacter and do game.Players:GetPlayerFromCharacter(hit.Parent). It's much more reliable, and no need to do if hit and hit.Parent and ... then

1 answer

You missed "s" letter on line 3 and all your script is scattered. Here is the right working script

local MPS = game:GetService("MarketplaceService")
local productId = 408797380

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			MPS:PromptProductPurchase(player, productId)
		end
	end
end)

You can read more here

4 comments

0

No need for line 5. You're just checking the same thing twice

0

But what if not an humanoid will hit that brick? (example the baseplate)

0

Yeah, don't listen to incapaz, you definitely want to check if its a person thats hitting the brick. In fact, I would go the extra mile and do a (hit.Parent:FindFirstChild('Humanoid') or hit.Parent.Parent:FindFirstChild('Humanoid')).

0

Why would you ever check the parent's parent. Every Player Humanoid part that can trigger Touched on BaseParts will be in the hierarchy format to reach Humanoid by simply using FindFirstChild() on Parent.

This discussion is preserved as it was. New answers, comments, and votes are closed.