Hello, guys! I want to make a gamepass button but the problem is that the script I made doesn’t work. Please note that it is a local script.
local id = 38853048
local MPS = game:GetService("MarketplaceService")
local SS = game:GetService("ServerStorage")
local GC = SS:WaitForChild("GravityCoil")
script.Parent.MouseButton1Click:Connect(function(plr)
MPS:PromptGamePassPurchase(plr.UserId, id)
end)
game.Players.PlayerAdded:Connect(function(plr)
while wait() do
if MPS:UserOwnsGamePassAsync(plr.UserId, id) then
GC:Clone().Parent = plr.Backpack
end
end
end)
I tried searching devForum but I didn’t find any solutions yet.
Forgot to mention that I get this error on output: Players.megamegaal.PlayerGui.ShopUI.Background.Frame.GravityCoil.BuyButton.Script:6: attempt to index nil with 'UserId'
you need to separate this into client-server scripts, you are also missing the bit of code for server for finalizing purchases
you also should not while wait check and clone like what ur doing, on playeradded, merely check once on server that if own gamepass give (and do it on the server).
local id = 38853048
local MPS = game:GetService("MarketplaceService")
local SS = game:GetService("ServerStorage")
local GC = SS:WaitForChild("GravityCoil")
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if not MPS:UserOwnsGamePassAsync(plr.UserId, id) then
MPS:PromptGamePassPurchase(plr.UserId, id)
end
end)
while wait() do
if MPS:UserOwnsGamePassAsync(plr.UserId, id) then
script.Parent.Text = "Owned"
end
end
and I added a server script in ServerScriptService:
local id = 38853048
local MPS = game:GetService("MarketplaceService")
local SS = game:GetService("ServerStorage")
local GC = SS:WaitForChild("GravityCoil")
game.Players.PlayerAdded:Connect(function(plr)
if MPS:UserOwnsGamePassAsync(plr.UserId, id) then
GC:Clone().Parent = plr.Backpack
end
end)
But it still doesn’t work. So I don’t know the actual script.
you are on the right step just add the following code to your server script:
local MarketplaceService = game:GetService("MarketplaceService")
local function gamepassPurchaseFinished(player, GamePassId, WasPurchased)
if GamePassId==id and WasPurchased then
GC:Clone().Parent = plr.Backpack
end
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)
I would also suggest getting rid of the while wait() loop, you should instead try to receive the info on an event like remoteevent or childadded in your backpack.