Hello everyone! In this tutorial, I will show you guys how to make a Membership System. This means a player can purchase a membership with robux and the membership only lasts for a specific amount of time (1 Month, 1 Year, etc.).
Step 1: GUI
First off, use this GUI and put it in StarterGui. This is the default layout and you can change it however you like. Just make sure not to change scripts unless you are sure of what you’re doing! Also try not to change names or it could get confusing in the future steps.
Step 2: Create Products
Go to your game’s configure page and go to Developer Products. When you are there, create a new product called whatever you like, determine the price, and add an image for it (Image is required). When you created it, it should look something like this:
Step 3: Configuring Scripts
If you go into the GUI from step 1 and go into the TextButton called “Buy”, there is a LocalScript. Open that script so that we can configure it.
Once you open the script, you will see a variable called ProductId
. Change that to the ID of the product you created in the last step. Code Example:
local MarketPlaceService = game:GetService("MarketplaceService")
local ProductId = 1067787625 -- Change this to the Product Id
script.Parent.MouseButton1Click:Connect(function()
MarketPlaceService:PromptProductPurchase(game.Players.LocalPlayer,ProductId)
end)
Step 4: The main script
Create a Script in ServerScriptService and call it whatever you like. Create the following variables in the script:
local DataStoreService = game:GetService("DataStoreService")
local MembershipData = DataStoreService:GetDataStore("MembershipData")
local MarketPlaceService = game:GetService("MarketplaceService")
local ProductId = 1067787625 -- Change to id
local MembershipDuration = 2628000 -- The duration of the membership (In seconds). It is default set to 1 month.
After that, we will create a PlayerJoined
event to load the Membership data. The Membership data is the os.time()
value that the Membership ends. Add this code below the code we made above:
game.Players.PlayerAdded:Connect(function(Player)
-- Creates a folder in the Player containing the Player's Membership data
local Folder = Instance.new("Folder",Player)
Folder.Name = "Membership"
local IsMember = Instance.new("BoolValue",Folder)
IsMember.Name = "IsMember"
local MembershipTime = Instance.new("IntValue",Folder)
MembershipTime.Name = "MembershipTime"
-- Gets membership data. Data will be the os.time() the membership expires
local MembershipInfo
local Success,Error = pcall(function()
MembershipInfo = MembershipData:GetAsync(Player.UserId.."-MembershipInfo")
end)
-- Loads data
if not MembershipInfo then MembershipInfo = 0 end -- Sets to 0 if no data given
MembershipTime.Value = MembershipInfo
if os.time() <= MembershipInfo then
IsMember.Value = true
else
IsMember.Value = false
end
while true do
wait(30)
if os.time() <= MembershipInfo then
IsMember.Value = true
else
IsMember.Value = false
end
end
end)
Now that the data loads when the player joins the game, we have to make it so when the player purchases the membership, it saves the time until the membership ends. To make this work, we have to add this code below:
MarketPlaceService.ProcessReceipt = function(PurchaseInfo)
print("Purchase Processing...")
local Player = game:GetService("Players"):GetPlayerByUserId(PurchaseInfo.PlayerId)
if PurchaseInfo.ProductId == ProductId then
-- Get info
local MembershipTime = Player.Membership.MembershipTime.Value
-- Save info
local UntilTime
-- Checks if user already purchased membership in case
if os.time() <= MembershipTime then
UntilTime = MembershipTime + MembershipDuration
else
UntilTime = os.time() + MembershipDuration
end
local Success,Error = pcall(function()
MembershipData:SetAsync(Player.UserId.."-MembershipInfo",UntilTime)
end)
-- Checks if successful
if Success then
Player.Membership.IsMember.Value = true
Player.Membership.MembershipTime.Value = UntilTime
print("Purchase Granted!")
return Enum.ProductPurchaseDecision.PurchaseGranted
else
-- If there is an error, the purchase is not processed yet and it prints the error.
print("Error while purchasing membership: "..Error)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
end
We are now pretty much done! Here is what your script should look like all together:
local DataStoreService = game:GetService("DataStoreService")
local MembershipData = DataStoreService:GetDataStore("MembershipData")
local MarketPlaceService = game:GetService("MarketplaceService")
local ProductId = 1067787625 -- Change to id
local MembershipDuration = 2628000 -- The duration of the membership (In seconds). It is default set to 1 month.
-- Player Membership Info
game.Players.PlayerAdded:Connect(function(Player)
-- Creates a folder in the Player containing the Player's Membership data
local Folder = Instance.new("Folder",Player)
Folder.Name = "Membership"
local IsMember = Instance.new("BoolValue",Folder)
IsMember.Name = "IsMember"
local MembershipTime = Instance.new("IntValue",Folder)
MembershipTime.Name = "MembershipTime"
-- Gets membership data. Data will be the os.time() the membership expires
local MembershipInfo
local Success,Error = pcall(function()
MembershipInfo = MembershipData:GetAsync(Player.UserId.."-MembershipInfo")
end)
-- Loads data
if not MembershipInfo then MembershipInfo = 0 end -- Sets to 0 if no data given
MembershipTime.Value = MembershipInfo
if os.time() <= MembershipInfo then
IsMember.Value = true
else
IsMember.Value = false
end
while true do
wait(30)
if os.time() <= MembershipInfo then
IsMember.Value = true
else
IsMember.Value = false
end
end
end)
-- Purchase Handler
MarketPlaceService.ProcessReceipt = function(PurchaseInfo)
print("Purchase Processing...")
local Player = game:GetService("Players"):GetPlayerByUserId(PurchaseInfo.PlayerId)
if PurchaseInfo.ProductId == ProductId then
-- Get info
local MembershipTime = Player.Membership.MembershipTime.Value
-- Save info
local UntilTime
-- Checks if user already purchased membership in case
if os.time() <= MembershipTime then
UntilTime = MembershipTime + MembershipDuration
else
UntilTime = os.time() + MembershipDuration
end
local Success,Error = pcall(function()
MembershipData:SetAsync(Player.UserId.."-MembershipInfo",UntilTime)
end)
-- Checks if successful
if Success then
Player.Membership.IsMember.Value = true
Player.Membership.MembershipTime.Value = UntilTime
print("Purchase Granted!")
return Enum.ProductPurchaseDecision.PurchaseGranted
else
-- If there is an error, the purchase is not processed yet and it prints the error.
print("Error while purchasing membership: "..Error)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
end
Also, if your script is not working in studio, make sure you have API services enabled (Can be found in game settings)
Alright, that is basically it for this tutorial, I hope it helped you out! If it did, please make sure to reply on how useful this was. If there is a problem, please reply with your problem and I will try to help you out as soon as possible! Also please leave a like if you enjoy
Was this tutorial useful?
- Yes
- In the future maybe
- No
0 voters
Also, please comment any tutorial ideas!