Problem with script!

So my script should only give this box if you have the gamepass but for somereason it will give the box and promt if you dont have to gamepass does anyone know the problem?

system = script.Parent

model = system.Box --

backup = model:Clone()

regen = system.Regen

local gamepassId = 148181594
local market = game:GetService("MarketplaceService")

local function checkRegen(plr)
	if regen.Value == 1 then

		if market:UserOwnsGamePassAsync(plr.UserId,gamepassId) then

			wait(1)

			model = backup:Clone()

			model.Parent = system

			model:MakeJoints()

		else
			market:PromptGamePassPurchase(plr, gamepassId)
		end

	end
end

game.Players.PlayerAdded:Connect(function(plr)



end)



regen.Changed:Connect(function()
	for i,v in next, game:GetService("Players"):GetPlayers() do
		checkRegen(v)
	end
end)
``
2 Likes

Can you add a few prints and show the output results?

Hey i think i see the problem,

You are never calling your function.
Call it in the playerAdded block and see if that solves it.

EDIT: I see now u called it in the loop. I think that one is running before the player is actually in place. Iā€™d still keep it as a fail safe thoo.

try changing the function to this:

local function checkRegen(plr)
	if market:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
		if regen.Value == 1 then
			wait(1)
			model = backup:Clone()
			model.Parent = system
			model:MakeJoints()
		end
	else
		market:PromptGamePassPurchase(plr, gamepassId)
	end
end

I know butit will always spawn for some reason even if you dont have the gamepass

I would try adding a pcall and printing out the error message if there is one. If it prints out the error, tell me what it says please.


local function checkRegen(plr)
    if regen.Value == 1 then
        local owned
        local success, errorMessage = pcall(function()
            owned = market:UserOwnsGamePassAsync(plr.UserId, gamepassId)
        end)

        if success and owned then
            task.wait(1)
            model = backup:Clone()
            model.Parent = system
            model:MakeJoints()
        else
            if errorMessage then
                warn(errorMessage)
            elseif not owned then
                warn("Player does not own the gamepass", plr.Name.." "..plr.UserId)
            end
        end
    else
        market:PromptGamePassPurchase(plr, gamepassId)
    end
end

You should also call the function in the PlayerAdded event. Not necessary, but recommended, and also depending on what you want.