This is a code redeeming script, it works perfectly fine until I leave the game right after I redeem a code… Some people told me to turn them into JSON and some told me to use BindToClose, which one should I use?
local data = game:GetService("DataStoreService"):GetDataStore("savedcodes")
local ss = game:GetService("ServerStorage")
local activated = {}
game.Players.PlayerAdded:Connect(function(player)
local key = "codes-"..player.userId
local folder = Instance.new("Folder",player)
folder.Name = "Codes"
local save = data:GetAsync(key)
if save then
for i = 1, #save do
local temp = Instance.new("BoolValue",folder)
temp.Name = save[1]
end
end
end)
game.ReplicatedStorage.Remote.Redeem.OnServerEvent:Connect(function(player,code)
local reward = player:FindFirstChild("leaderstats")
local r = reward.Coins
local key = "codes-"..player.userId
player.PlayerGui:WaitForChild("MainGui"):WaitForChild("Code")
if code == "Code1" then
if player.Codes:FindFirstChild(code) == nil then
local bool = Instance.new("BoolValue")
bool.Parent = player:FindFirstChild("Codes")
bool.Name = code
r.Value = r.Value + 300
player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = "Redeemed!"
wait(2)
player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = ""
else
player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = "Already Redeemed!"
wait(1)
player.PlayerGui:FindFirstChild("MainGui").Code.TextBox.Text = ""
end
end
for i,v in pairs(player.Codes:GetChildren()) do
if v:isA("BoolValue") then
table.insert(activated, v.Name)
data:SetAsync(key,activated)
wait(60)
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
data:SetAsync("codes-"..plr.userId, activated)
end)