Lua Tutorial 3 - June 5th, 2024

An advanced gamepass purchase system with a reward
 avatar
unknown
lua
9 months ago
2.2 kB
4
Indexable
-- today i will be making a fully function and advanced gamepass system
-- this is used to make the player buy a gamepass in order to achieve something


-- server script

local WS = game:GetService("Workspace")
local RS = game:GetService("ReplicatedStorage")
local MPS = game:GetService("MarketplaceService")
local gamepassID = 123456789 -- gamepass ID
local partToTouch = WS["Shop"]

partToTouch["Touched"]:Connect(function(hit) -- connects the function when the part is touched
  
  local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) -- gets the player from the touch function
  
  if player then
    if MPS:UserOwnsGamePassAsync(player.UserId, gamepassID) then -- checks to see if player owns the gamepass
      warn("Player already owns selected gamepass") -- if player already owns the gamepass, then it warns the console
    else
      MPS:PromptGamePassPurchase(player.UserId, gamepassID) -- prompts the gamepass purchase if the player does not already own it
    end
  else
    warn("Not a player") -- if the object that touched the part is not a player, then it warns the console
  end
end)

-- local script

local player = game:GetService("Players")["LocalPlayer"]
local ownsGamepass = player:GetAttribute("ownsGamepass")
player:SetAttribute("ownsGamepass", false) -- defaults the value to false for that player until player owns gamepass

local WS = game:GetService("Workspace")
local itemToRecieve = WS["Reward"]
local gamepassID = 123456789 -- gamepass ID


while task.wait(0.1) do
  if not ownsGamepass then -- checks that the player infact does not own the gamepass
    if MPS:UserOwnsGamePassAsync(player.UserId, gamepassID) then
      itemToRecieve:Clone() Clone.Parent = player.Backpack -- adds the reward to the players inventory
      
      player:SetAttribute("ownsGamepass", true) -- sets the value to true and stops the "while, do" task
    else
      -- removed because it would clog the output with warnings
    end
  else
    -- also removed to prevent output clogging
  end
end

-- this sytem is very efficient and useful for prompting the player to purchase a gamepass, and then giving the player an item if they own the gamepass
      
Editor is loading...
Leave a Comment