Need help with regen code

Hello fellow devs of Roblox, need some hopefully basic scripting help. I am trying to make a respawn button that only works if the player has a gamepasss, the problem is, I can’t get it to work. The model just respawns as if you had the gamepass anyways. I tried looking around Discord, and Google and trying to modify the code. here is the code I have inside the part. Inside the part, there is a click detector, the part is then grouped up with the other models in one larger model. Pretty much the tree is the main model, the stuff I’m trying to regen, and the part with the script.

local MarketPlaceService = game:GetService("MarketplaceService")

local success,exists = pcall(function()
 return MarketPlaceService.UserOwnsGamePassAsync(player.UserID, 11074685)
end)
if exists then
  model = script.Parent.Parent
backup = model:clone()
debounce = true
clickDetector = script.Parent.ClickDetector

function regenerate()
script.Parent:Destroy()
wait(1)
model = backup:clone()
model.Parent = game.Workspace
model:MakeJoints()
end

function onMouseClick(player)
debounce = false
regenerate()
wait(5)
debounce = true
end
else
  MarketPlaceService.PromptGamePassPurchase(player, gamepassID) 
end
clickDetector.MouseClick:connect(onMouseClick)

Hopefully, you guys can help me out with this weird Frankenstein of code.

1 Like

:UserOwnsGamepassAsync only yields, it does not throw. Meaning you do not need to use pcall with it.

Additionally, you should have two onMouseClick functions for whether or not the user owns the gamepass. Additionally, you should cache the resut of :UserOwnsGamepassAsync, and update the cache in the instance that the user does actually purchase the gamepass.

In the end you should have something like this:
*note that this does not handle in-game purchases, and that you should account for and handle this

local MarketPlaceService = game:GetService("MarketplaceService")
local gamepassID = 11074685
local ownsGamepass = MarketPlaceService:UserOwnsGamePassAsync(player.UserID, gamepassID) -- update value on gamepass purchased through server side

local debounce = true

local model = script.Parent.Parent
local backup = model:clone()
local clickDetector = script.Parent:WaitForChild("ClickDetector")

local function regenerate()
	script.Parent:Destroy()
	wait(1)
	model = backup:clone()
	model.Parent = game.Workspace
	model:MakeJoints()
end

clickDetector.MouseClick:Connect(function()
	debounce = false
	
	if ownsGamepass then
		regenerate()
	else
		MarketPlaceService.PromptGamePassPurchase(player, gamepassID) 
	end
	
	wait(5)
	
	debounce = true
end)

does that mean the script will only work after they rejoin the game
like, the buy the gamepass ingame, they have to rejoin or something else? talking about the note at the end.

could you add what you mentioned in your note

I got the issue resolved now, thanks for those who helped at most they can, I am now marking this as resolved.