I am makeing a promo codes system with datastore 2 I Need Help with makeing a Backup System.
Here is my code
--// promocodehandler.lua
local codes = require(script.Parent.Codes)
local players = game:GetService("Players")
local datastore2 = require(1936396537)
--
datastore2.Combine("masterKey1", "Codes") -- this is for our promocode system.
datastore2.Combine("masterKey2", "Coins", "Gems") -- this is for your stats.
--
local redeem_remote = game.ReplicatedStorage:WaitForChild("RedeemCode")
--
local stats_storage = Instance.new("Folder")
stats_storage.Name = "Stats Storage"
stats_storage.Parent = game.ServerStorage
--
_G["Promocodes Storage"] = {}
--
function dateStringToNumber(str)
-- mm/dd/yyyy format
local tbl = {}
for _, strNum in next, str:split("/") do
table.insert(tbl, tonumber(strNum))
end
return tbl
end
--
local codes_functions = {
["Coins"] = function(player, amount)
amount = amount or 0
local cd = datastore2("Coins", player)
cd:Increment(amount)
end,
["Gems"] = function(player, amount)
amount = amount or 0
local gd = datastore2("Gems", player)
gd:Increment(amount)
end,
}
local function playerAdded(player)
--// stats
local gd = datastore2("Gems", player)
local cd = datastore2("Coins", player)
local stats = Instance.new("Folder")
stats.Name = player.Name
stats.Parent = stats_storage
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = stats
coins.Value = cd:Get(0)
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = stats
gems.Value = gd:Get(0)
--// fake leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gl = Instance.new("IntValue")
gl.Name = "Gems"
gl.Value = gems.Value
gl.Parent = leaderstats
local cl = Instance.new("IntValue")
cl.Name = "Coins"
cl.Value = gems.Value
cl.Parent = leaderstats
--// datastore2 (stats) events
gd:OnUpdate(function(newStats)
gems.Value = newStats
gl.Value = newStats
end)
cd:OnUpdate(function(newStats)
coins.Value = newStats
cl.Value = newStats
end)
--// promocodes
_G["Promocodes Storage"][player.Name] = {}
local promocodes = datastore2("Codes", player)
for _, code in next, promocodes:GetTable({}) do
table.insert(_G["Promocodes Storage"][player.Name], code)
end
promocodes:OnUpdate(function(newValue)
_G["Promocodes Storage"][player.Name] = newValue
end)
end
for _, player in next, players:GetPlayers() do
playerAdded(player)
end
--
players.PlayerAdded:Connect(playerAdded)
--
players.PlayerRemoving:Connect(function(player)
_G["Promocodes Storage"][player.Name] = nil
if stats_storage:FindFirstChild(player.Name) then
stats_storage[player.Name]:Destroy()
end
end)
--
redeem_remote.OnServerInvoke = function(player, code)
code = code:lower() or ""
local current_time = os.date("*t", os.time())
print("current time:",current_time)
local user_promocodes = datastore2("Codes", player)
if codes[code] then
local rewards = codes[code]["Rewards"] or {}
local expiration_date = dateStringToNumber(codes[code]["Expired_Date"])
print(code,"expiration date:",expiration_date)
if current_time.month <= expiration_date[1] and current_time.day <= expiration_date[2] and current_time.year <= expiration_date[3] then
if not table.find(user_promocodes:Get(), code) then
for stats, amount in next, rewards do
if codes_functions[stats] then
codes_functions[stats](player, amount)
end
end
local modified = user_promocodes:Get()
table.insert(modified, code)
user_promocodes:Set(modified)
print("modified promocode data:", modified)
return "Successfully redeemed!", Color3.fromRGB(98, 255, 0)
else
return "Already redeemed!", Color3.fromRGB(255, 213, 0)
end
else
return "Expired promocode!", Color3.fromRGB(255, 213, 0)
end
else
return "Invalid promocode!", Color3.fromRGB(255, 46, 46)
end
end