I have two data storages in my game, one for saving the player’s coins, and the other is for a daily login bonus.
For some reason, the daily login bonus is saving the coins you get for that day, but when I purchase coins in-game, they don’t save?
COIN DATA STORAGE
local DataStoreService = game:GetService("DataStoreService")
local StarcoinsDataStore = DataStoreService:GetDataStore("StarcoinsDS")
local plr = game.Players.LocalPlayer
local debounce = false
game.Players.PlayerAdded:Connect(function(player)
local Currency = Instance.new("Folder")
Currency.Name = "Currency"
Currency.Parent = player
local Starcoins = Instance.new("NumberValue")
Starcoins.Name = "Starcoins"
Starcoins.Parent = Currency
local playerUserId = "player: "..player.UserId
local data = {
Starcoins = player.Currency.Starcoins.Value;
}
local success, errormessage = pcall(function()
data = StarcoinsDataStore:GetAsync(playerUserId)
end)
if success == true then
if data then
Starcoins.Value = data.Starcoins
print("Starcoins Data loaded for "..player.Name)
print(Starcoins.Value)
else
Starcoins.Value = 0
end
end
end)
function savestarcoinsdata(player)
local playerUserId = "player: "..player.UserId
local data = {
Starcoins = player.Currency.Starcoins.Value;
}
local success, errormessage = pcall(function()
if data then
StarcoinsDataStore:SetAsync(playerUserId, data)
else
print("No Data")
end
end)
if success == true then
print("Starcoins saved successfully! for "..player.Name)
else
print("Data did not save")
warn(errormessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
savestarcoinsdata(player)
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetChildren()) do
savestarcoinsdata(v)
end
end)
DAILY LOG IN BONUS
local dss = game:GetService("DataStoreService")
local dailyRewardDS = dss:GetDataStore("DailyRewards")
local rewardsForStreak =
{
[1] = 10,
[2] = 15,
[3] = 20,
[4] = 25,
[5] = 30,
[6] = 35,
[7] = 40,
}
game.Players.PlayerAdded:Connect(function(plr)
wait(3)
if plr:IsInGroup(8182291) then
print ("Player is in the group.")
local success, dailyRewardInfo = pcall(function()
return dailyRewardDS:GetAsync(plr.UserId .. "-DailyRewards")
end)
if type(dailyRewardInfo) ~= "table" then dailyRewardInfo = {nil, nil, nil} end
local leaderstats = plr.Currency
local cash = plr.Currency.Starcoins
cash.Value = dailyRewardInfo[1] or 0
cash.Parent = leaderstats
local lastOnline = dailyRewardInfo[2]
local currentTime = os.time()
local timeDifference
if lastOnline then
timeDifference = currentTime - lastOnline
end
if not timeDifference or timeDifference >= 24*60*60 then
local streak = dailyRewardInfo[3] or 1
local reward = rewardsForStreak[streak]
local dailyRewardGui = plr.PlayerGui:WaitForChild("DailyRewardGui")
local mainGui = dailyRewardGui.Frame:WaitForChild("MainGui")
local claimBtn = mainGui.Claim:WaitForChild("ClaimButton")
local rewardLabel = mainGui:WaitForChild("RewardLabel")
rewardLabel.Text = reward
dailyRewardGui.Enabled = true
claimBtn.MouseButton1Click:Connect(function()
cash.Value = cash.Value + reward
mainGui:TweenPosition(UDim2.new(0.5,0,1.6,0), "Out", "Bounce", 1)
wait(1)
dailyRewardGui.Enabled = false
local streak = streak + 1
if streak > 7 then streak = 1 end
local success, errormsg = pcall(function()
dailyRewardDS:SetAsync(plr.UserId .. "-DailyRewards", {cash.Value, os.time(), streak})
end)
end)
elseif timeDifference then
wait((24*60*60) - timeDifference)
if game.Players:FindFirstChild(plr) then
local streak = dailyRewardInfo[3] or 1
local reward = rewardsForStreak[streak]
local dailyRewardGui = plr.PlayerGui:WaitForChild("DailyRewardGui")
local mainGui = dailyRewardGui:WaitForChild("MainGui")
local claimBtn = mainGui:WaitForChild("ClaimButton")
local rewardLabel = mainGui:WaitForChild("RewardLabel")
rewardLabel.Text = reward
dailyRewardGui.Enabled = true
claimBtn.MouseButton1Click:Connect(function()
cash.Value = cash.Value + reward
dailyRewardGui.Enabled = false
local streak = streak + 1
if streak > 7 then streak = 1 end
pcall(function()
dailyRewardDS:SetAsync(plr.UserId .. "-DailyRewards", {cash.Value, os.time(), streak})
end)
end)
end
end
end
end)