So im trying to script some gamepasses and i have multiple gamepasses and i don’t know how to handle them on the server so lets say i buy a sword how could i be able to handle it on the server and get the id without mixing it up between other gamepasses:
this is the code i currently have for the client:
local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShopUi = script.Parent
local OpenGamepassFrame = script.Parent.GamepassImage
local ShopButton = script.Parent.Parent.ShopButton
local RequestGamepassTool = ReplicatedStorage.Remotes.Event:WaitForChild("RequestGamepassTool")
local player = Players.LocalPlayer
local currentConnection -- Store the current BuyButton connection
ShopButton.Activated:Connect(function()
ShopUi.Visible = not ShopUi.Visible
ShopUi.Misc.Top.XButton.Visible = ShopUi.Visible
end)
local function PromptGamepass(gamePassId)
local hasPass
local success, err = pcall(function()
hasPass = MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamePassId)
end)
if success then
if hasPass then
print("You already own this gamepass")
else
MarketPlaceService:PromptGamePassPurchase(player, gamePassId)
end
end
end
for _, gamepasses in ipairs(ShopUi.GamePasses:GetChildren()) do
if gamepasses:IsA("ImageButton") then
gamepasses.Activated:Connect(function()
script.Parent.MouseClick:Play()
OpenGamepassFrame.GamepassName.Text = gamepasses.GamepassName.Value
OpenGamepassFrame.GamepassPrice.Text = gamepasses.GamepassPrice.Value .. "R$"
OpenGamepassFrame.GamepassDescription.Text = gamepasses.GamepassDescription.Value
OpenGamepassFrame.Image = gamepasses.Image
if currentConnection then
currentConnection:Disconnect()
end
currentConnection = OpenGamepassFrame.BuyButton.Activated:Connect(function()
PromptGamepass(gamepasses.GamepassId.Value)
end)
end)
end
end
ShopUi.Misc.Top.XButton.Activated:Connect(function()
ShopUi.Visible = false
ShopUi.Misc.Top.XButton.Visible = false
end)
(please help me i have been trying for so long to figure it out but yet i can’t)
Could you elaborate on how do you want to handle multiple gamepasses? Like checking on server if you just bought the gamepass in-game and organize gamepass ids and its function?
You can try this server script. Just remember to edit the ToolItems, GamepassIds, and ItemsBought variable so it fits your game. The script does accurate checking via the ToolItems and GamepassIds tables, so there’s no need to worry about items being mixed up.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local ToolItems = {
ReplicatedStorage:WaitForChild("Tool"), -- add tool locations here , make sure the order of the tools match the order of the gamepass ids
ReplicatedStorage:WaitForChild("Tool2")}
local GamepassIds =
{0,
0} -- add your gamepass id's here (only if they give tools)
local ItemsBought = {
false,
false} -- only change the number of falses in the list to be the same as the number of gamepass ids
local function givePlayerTools(Player)
for i, GamepassId in pairs(GamepassIds) do
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassId) or ItemsBought[i] == true then
if not Player.Character or Player.Character.Humanoid.Health <= 0 then
Player.CharacterAdded:Wait()
end
ToolItems[i]:Clone().Parent = Player:WaitForChild("Backpack")
end
end
end
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
givePlayerTools(Player)
end)
if Player.Character then
givePlayerTools(Player)
end
end)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, Id, Purchased)
if Purchased == true then
for i, GamepassId in pairs(GamepassIds) do
if GamepassId == Id then
ToolItems[i]:Clone().Parent = Player:WaitForChild("Backpack")
ItemsBought[i] = true
end
end
end
end)
It’s just a check to know the player bought the gamepass when the player respawns. MarketplaceService:UserOwnsGamePassAsync() seems to never (in my experience) update when the user buys the gamepass mid-game, therefore it gives false when the player respawns. ItemsBought is a way to fix that issue.
No, keep all the values in that table false. Just make sure that the number the values in that table are the same as the number of values in GamepassIds and ToolItems. Examples shown below:
local ToolItems = {
ReplicatedStorage:WaitForChild("Tool"),
ReplicatedStorage:WaitForChild("Tool2")} -- two items, so two gamepasses in GamepassIds and two fasles in ItemsBought
local GamepassIds =
{0,
0}
local ItemsBought = {
false,
false}
local ToolItems = {
ReplicatedStorage:WaitForChild("Tool"), -- only one item here, so only one gamepass in GamepassIds and one fasle in ItemsBought
local GamepassIds =
{0}
local ItemsBought = {
false}
Try setting attributes to the buttons with the id or create a table to store each buttons name and their gamepass id prefer the first one and getting the attribute. (I would have given an example but its currently night.)
Make sure that you use the number type and not the string type of the attribute.
PS: Remember that the order of the gamepass ids correlate to the order of the tools, so make sure that your tool’s order is the table matches with the gamepass id in that same order. I don’t think I made that so clear in the script notes, so here’s a quick example below to help visualize what I mean:
local ToolItems = {
ReplicatedStorage:WaitForChild("Tool"), -- this tool matches with gamepass id 0
ReplicatedStorage:WaitForChild("Tool2")} -- this tool matches with gamepass id 1
ReplicatedStorage:WaitForChild("Tool3"), -- this tool matches with gamepass id 2
local GamepassIds =
{0,
1,
2,}
Back i opened studio to give some code examples would you like them?
local frame = script.Parent
for i, v in pairs(frame:GetChildren()) do
if v:IsA("ImageButton") then
local id = v:GetAttribute("id")--the gamepass ID
local itemName = v:GetAttribute("it")--the name of said item
if id and itemName then
--this code is an example of how you can use attributes
local items = game.ReplicatedStorage:WaitForChild("items")
if items:FindFirstChild(itemName) then
end
end
end
end
Alternatively you could use this code to easily get the item through the path
Anyways see you later as it is night for me.
local items = game.ReplicatedStorage:WaitForChild("items")
local gamepasses = {
{3000000000, items:WaitForChild("itemnamehere")},
{33919, items:WaitForChild("itemnamehere")},
}
local function retrieveID(a)
return gamepasses[a][1]
end
local function retrieveITEM(a)--[[a is a Number]]
return gamepasses[a][2]
end
print(retrieveID(1))
print(retrieveID(2))