Gamepass script help?

I have an image button in my character selection gui. It all works fine, but I would like to know how to make it so you only spawn if you own the gamepass, but it’s spawning them even if they don’t own the gamepass.

DISCLAIMER i don’t want to delete the button if they don’t own the gamepass, i want it to still show and just prompt the purchase if they don’t own it. thankd

local script inside image button:

local	Characters = game:GetService("ReplicatedStorage").Characters
local	Event = game:GetService("ReplicatedStorage").SetSubject
local mps = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local gpId = 9066924
local button = script.Parent

for i,v in pairs(script.Parent.Parent:GetChildren()) do
	button.MouseButton1Click:Connect(function()
		if Characters:FindFirstChild(v.Name) then
			if mps:UserOwnsGamePassAsync(player.UserId, gpId) then
			Event:FireServer(v.Name)
			elseif not mps:UserOwnsGamePassAsync(player.UserId, gpId) then
				mps:PromptGamePassPurchase(player, gpId)
			end
		end
	end)
end
	

Gamepasses should generally be checked from the server and not the client just to be safe from exploiters. Also what do you mean “spawn in”?

there’s a character selection gui, so when they click a button they become a character.
i don’t want them to become the character unless they own the gamepass.

1 Like

video of what i mean by ‘spawn in’

https://gyazo.com/a9ced81e79b5575080b9595a338e4a0f

Can you try this code?

local Characters = game:GetService("ReplicatedStorage").Characters
local Event = game:GetService("ReplicatedStorage").SetSubject
local MarketplaceService = game:GetService("MarketplaceService")
local Player = game.Players.LocalPlayer
local gamePassId = 9066924
local Button = script.Parent

for i,v in pairs(script.Parent.Parent:GetChildren()) do
	Button.MouseButton1Click:Connect(function()
		if Characters:FindFirstChild(v.Name) then
             local OwnsGamepass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamePassId)
             if OwnsGamepass then
                   Event:FireServer(v.Name)
             elseif not OwnsGamepass then
		           MarketplaceService:PromptGamePassPurchase(Player, gamePassId)
	         end
		 end
	end)
end

tried, still getting same issue. the character spawns and then the gamepass purchase is prompted

Can you please print(OwnsGamepass) ?

i fixed it

i realised i had a duplicated version of the OLD script without a gamepass lock on inside the button, sorry for causing trouble