-
-
Save TheBrainy06/b413a789d8d3cf870ef3863584799696 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--// Services | |
local MarketplaceService = game:GetService("MarketplaceService") | |
local DataStoreService = game:GetService("DataStoreService") | |
local TeleportService = game:GetService("TeleportService") | |
local HttpService = game:GetService("HttpService") | |
local Players = game:GetService("Players") | |
--// Variables | |
local vipDataStore = DataStoreService:GetDataStore("PlayerVIPData") | |
local vipConfig = require(script.Config) | |
--// Constants | |
local POSSIBLE_ERRORS = { | |
tierDoesNotExist = "Tier with name '%s' does not exist.", | |
playerHasServer = "Player already owns a VIP server.", | |
serverHasExpired = "VIP server has expired!" | |
} | |
local SERVER_CODE_FORMAT = "%s-%s" | |
--// Local Functions | |
local function ReturnTierConfig(serverTier: string) | |
return (vipConfig.Tiers[serverTier] and vipConfig.Tiers[serverTier] or nil) | |
end | |
-- Function to generate a server code which can be used to join VIP servers | |
local function GenerateServerCode(plrUserId: number) | |
local randomId = HttpService:GenerateGUID(false) | |
local trimmedId = randomId:sub(1, 5) | |
return SERVER_CODE_FORMAT:format(plrUserId, trimmedId) | |
end | |
-- Used to fetch expiry date from the server validity | |
local function GetExpiry(serverValidity: number) | |
local daysToSeconds = (serverValidity * 86400) | |
local currentTime = os.time() | |
return (currentTime + daysToSeconds) | |
end | |
-- Used to check if a VIP server has expired | |
local function HasExpired(expiryTime: number) | |
local currentTime = os.time() | |
return (currentTime - expiryTime >= 0) and true or false | |
end | |
-- Function to return a private server code for teleporting | |
local function GetPrivateServerCode() | |
local generatedCode | |
local success, response = pcall(function() | |
generatedCode = TeleportService:ReserveServer(game.PlaceId) | |
end) | |
return generatedCode | |
end | |
local function DoesTierExistUsingProductId(productId: number) | |
for i, v in pairs(vipConfig.Tiers) do | |
if v.ProductId == productId then | |
return i | |
end | |
end | |
end | |
local vipServer = {} | |
-- Function to check whether a player owns a VIP server. Returns whether they own a VIP server as well as the server info if applicable. | |
function vipServer:DoesPlayerHaveVipServer(plr: Player) | |
local plrUserId = plr.UserId | |
local fetchedData | |
local success, response = pcall(function() | |
fetchedData = vipDataStore:GetAsync(plrUserId) | |
end) | |
return (fetchedData ~= nil and true or false), fetchedData | |
end | |
-- A function used internally to remove the server info from the datastore once a player's VIP server has expired. | |
function vipServer:ClearServerData(plrUserId: number) | |
local success, response = pcall(function() | |
vipDataStore:RemoveAsync(plrUserId) | |
end) | |
return success | |
end | |
-- A function to prompt a purchase screen for the player to purchase a VIP server. | |
function vipServer:PromptPurchase(plr: Player, serverTier: string) | |
local tierData = ReturnTierConfig(serverTier) | |
if not tierData then warn(POSSIBLE_ERRORS.tierDoesNotExist:format(serverTier)) return end | |
MarketplaceService:PromptProductPurchase(plr, tierData.ProductId) | |
end | |
-- A function which creates a VIP server depending on the tier. | |
function vipServer:CreateServer(plr: Player, serverTier: string) | |
local tierConfig = ReturnTierConfig(serverTier) | |
local plrHasServer = vipServer:DoesPlayerHaveVipServer(plr) | |
local plrUserId = plr.UserId | |
if not tierConfig then warn(POSSIBLE_ERRORS.tierDoesNotExist:format(serverTier)) return end | |
if plrHasServer then warn(POSSIBLE_ERRORS.playerHasServer) return end | |
local dataToSave = { | |
Tier = serverTier, | |
Code = GenerateServerCode(plrUserId), | |
TeleportCode = GetPrivateServerCode(), | |
Owner = plrUserId, | |
Expiry = GetExpiry(tierConfig.Validity) | |
} | |
local success, response = pcall(function() | |
vipDataStore:SetAsync(plrUserId, dataToSave) | |
end) | |
return success, dataToSave | |
end | |
-- A function to teleport to a VIP server using the code. | |
function vipServer:TeleportToServer(plr: Player, serverCode: string) | |
local serverOwnerId = tonumber(serverCode:split("-")[1]) | |
local serverData | |
local success, response = pcall(function() | |
serverData = vipDataStore:GetAsync(serverOwnerId) | |
end) | |
if success and serverData then | |
local hasExpired = HasExpired(serverData.Expiry) | |
if hasExpired then | |
warn(POSSIBLE_ERRORS.serverHasExpired) | |
vipServer:ClearServerData(serverOwnerId) | |
return | |
end | |
local wasSuccess, response = pcall(function() | |
TeleportService:TeleportToPrivateServer(game.PlaceId, serverData.TeleportCode, {plr}) | |
end) | |
if not wasSuccess then | |
warn(response) | |
end | |
end | |
end | |
MarketplaceService.ProcessReceipt = function(receiptInfo: any) | |
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId) | |
local doesTierExist = DoesTierExistUsingProductId(receiptInfo.ProductId) | |
local doesPlayerHaveServer = vipServer:DoesPlayerHaveVipServer(player) | |
if player and doesTierExist and not doesPlayerHaveServer then | |
local success, serverInfo = vipServer:CreateServer(player, doesTierExist) | |
if success then | |
return Enum.ProductPurchaseDecision.PurchaseGranted | |
end | |
end | |
if doesPlayerHaveServer then | |
warn(POSSIBLE_ERRORS.playerHasServer) | |
end | |
return Enum.ProductPurchaseDecision.NotProcessedYet | |
end | |
return vipServer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment