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?
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)
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