Module Script Blocks a script from running

Module Script

local marketPlaceService = game:GetService("MarketplaceService")

local function getGamePassPrice(Id)
    local info = marketPlaceService:GetProductInfo(Id, Enum.InfoType.GamePass)

    return info.PriceInRobux
end

local Gamepasses = {
    twoLifes = {
        Id = 67299,
        Price = getGamePassPrice(6770299) -- changing every privr to 100 instead of getting the real price fixes the bug
    },
    avenger = {
        Id = 6772263,
        Price = getGamePassPrice(6772263)
    },
    invincible = {
        Id = 677373,
        Price = getGamePassPrice(6778373)
    },
    doubledSpeed = {
        Id = 677385,
        Price = getGamePassPrice(677385)
    }
}

function Gamepasses.playerOwnsGamepass(player, gamepassId)
    if marketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
        return true
    else
        return false
    end
end

return Gamepasses

Script

local Gamepasses = require(game.ServerScriptService.Modules.GamepassesManager)
local gamepassId = 677820299  -- replace with the desired GamePass ID

local PlayerService = game:GetService("Players")

print(Gamepasses)
PlayerService.PlayerAdded:Connect(function(player)
	print("1")
	if Gamepasses:playerOwnsGamepass(player, gamepassId) then
		print("Player owns the specified GamePass.")
	else
		print("Player does not own the specified GamePass.")
	end
end)

print(Gamepasses)

seems to return the correct table with the game passes prices etc. however player added seems to not being fired as 1 is not printed.
Any thoughts?

3 Likes

Also, as I commented on the module script changing every price to 100 or a number fixes the bug and player added fires, printing 1.

I also replaced function Gamepasses.playerOwnsGamepass to function Gamepasses:playerOwnsGamepass.

However that doesn’t seem to fix the error anyway.

1 Like

I’m pretty sure this is due to the getGamePassPrice function yielding, creating a race condition where the player loads in faster than the module and PlayerAdded won’t detect the player. You can run PlayerAdded for every player that joined before the script loads:

local function PlayerAdded(player) -- function
print("1")
	if Gamepasses:playerOwnsGamepass(player, gamepassId) then
		print("Player owns the specified GamePass.")
	else
		print("Player does not own the specified GamePass.")
	end
end

for i, player in game.Players:GetPlayers() do 
-- loop through players already in game
PlayerAdded(player)
end
PlayerService.PlayerAdded:Connect(PlayerAdded)
3 Likes

Seems to work, thank you! :heart:, also can I put Module Scripts inside Serverstorage?

Yeah, they can run anywhere as long as it is required

1 Like

I think it works like:

Local Script → RemoteEvent = ServerScript → AccesToAnywhere with RemoteEvent since its ServerScript.

The issue in your code lies in the way you are calling the getGamePassPrice function when initializing the Gamepasses table

In Lua, when you define a table, the values are evaluated immediately when you call getGamePassPrice(6770299) to set the Price value for the twoLifes game pass, it will execute the function at that moment and assign the result to Price. However, at that point in time, the MarketplaceService is not available yet, as the script is still running and has not finished initializing.

To fix this issue, you can change the way you define the Gamepasses table by using a function to calculate the prices dynamically when needed

I hope that helped you:) :herb:

1 Like

how would that work? What can I do to make sure that the MarketplaceService is available? Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.