Hello, I’m not a good scripter, and I need help, I’m trying to make a gamepass give you an item in the replicatedstorage. My script isn’t working, so I was wondering if anyone can correct my mistake on these scripts. The prompt purchase works, but the script giving the item doesn’t. Can anyone help me?
I just woke up and am still very tired, but I believe you can only call :UserOwnsGamePassAsync() from the server.
:UserOwnsGamePassAsync() also caches the result, so you would need code to detect if they purchased it in game.
--Drop me in a server script
game:GetService("Players").PlayerAdded:Connect(function(player)
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 9409348) then
game:GetService("ReplicatedStorage"):FindFirstChild("M16A4"):Clone().Parent = player.Backpack
end
end)
Check this site out: Gamepass . It will provide you examples on what you are trying to achieve.
UPDATE: New Link
I don’t know about the UserOwnsGamePassAsync in the client, but you can’t clone a tool from a local script. Other players will not see that he has the tool. You can use PlayerAdded Instead.
I tried the script, but the results were still the same.
You put it in a server script?
Try to replace the FindFirstChild with WaitForChild.
game:GetService("Players").PlayerAdded:Connect(function(player)
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 9409348) then
game:GetService("ReplicatedStorage"):WaitForChild("M16A4"):Clone().Parent = player.Backpack --Replace it in his script.
end
end)
That’s MrLonely1221’s script but I changed it, I replaced FindFirstChild with WaitForChild.
And it won’t work in a local script, and it should be in the ServerScriptService
Yes, I did. I tried it as a local script and normal script.
Add this to process the purchases in game.
game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, gamePassId, purchased)
if not purchased then return; end
if player and gamePassId == 9409348 then
game:GetService("ReplicatedStorage"):FindFirstChild("M16A4"):Clone().Parent = player.Backpack
end
end)
@LightningLion58 The :FindFirstChild() will work because it’s already there on the server by the time the player joins.
The FindFirstChild just finds it as I know, the WaitForChild, Will get it if it’s there. and you said it’s there so that’s why.
:FindFirstChild() finds the first child with the name specified.
:WaitForChild() waits for the first child with the name.
They do the same thing, FindFirstChild() just errors if it’s not there, where WaitForChild() just waits for it until it can’t find it.
Did he buy the gamepass in the middle of the game? Because if he had it, and what you say is true, it should work then, right?
That’s what I was thinking which is why I gave him code to process that.
I buy it in studio with the test purchase feature.
UserOwnsGamepassAsync can only be used from the server, so consider using a RemoteEvent to fire the server in order to recieve the player on the server side in order to recieve the privilege right after purchasing, if not then use PlayerAdded to check. Note the player would have to rejoin for it to work.
How you would do this:
game.Players.PlayerAdded:Connect(function(player)
local ownsGamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 9409348)
-- ... rest of code here.
Did you read either of the code snippets I sent above? I sent both PlayerAdded, and PromptGamePassPurchaseFinished.
My code condensed into one script for you
game:GetService("Players").PlayerAdded:Connect(function(player)
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 9409348) then
game:GetService("ReplicatedStorage"):FindFirstChild("M16A4"):Clone().Parent = player.Backpack
end
end)
game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, gamePassId, purchased)
if not purchased then return; end
if player and gamePassId == 9409348 then
game:GetService("ReplicatedStorage"):FindFirstChild("M16A4"):Clone().Parent = player.Backpack
end
end)