Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)
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 User#19524 175 — 5y

1 answer

Log in to vote
2
Answered by
HaveASip 494 Moderation Voter
5 years ago

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

0
No need for line 5. You're just checking the same thing twice User#19524 175 — 5y
0
But what if not an humanoid will hit that brick? (example the baseplate) HaveASip 494 — 5y
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')). OrcaTheFish 95 — 5y
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. SummerEquinox 643 — 5y
Ad

Answer this question