I’ve recently come across a bug in my code that is preventing my daily login bonus from saving its added values. I am using DataStore2 and it has worked alright until now. For some reason, I am able to add experience from other functions in the game, but when I add experience using the daily login bonus it doesn’t save. I’ve even put in a print statement to test if the value is saved and it appears as though it has.
--My Experience Adder Script
function XPperLevel(level)
local XPNeeded = 1000 + 500 * level
return XPNeeded
end
local xpdebounces = {}
game.ServerStorage.AwardXP.Event:Connect(function(plr, experiencegiven)
while xpdebounces[plr] == true do
wait(1)
end
xpdebounces[plr] = true
local xpStore = DataStore2("xp", plr)
local levelStore = DataStore2("level", plr)
local levelold = levelStore:Get(1)
local level = levelold
local xp = xpStore:Get(0)
game.ReplicatedStorage.RemoteEvents.AddXP:FireClient(plr, experiencegiven, xp)
if not plr then
xpdebounces[plr] = false
return false
end
xpStore:Increment(experiencegiven)
local xp = xpStore:Get(0)
print('newxp is', xp)
local XPNeeded = XPperLevel(level)
while xp > XPNeeded do --while player's xp is greater than needed to level up
local LeftOverXP = xp-XPperLevel(level) --calculate amt left over
levelStore:Increment(1) --level + 1
level = level + 1
plr.Level.Value = level
xpStore:Increment(-XPNeeded) --player's xp set to leftover
xp = xp - XPNeeded
plr.XP.Value = xp
XPNeeded = XPperLevel(level)
end
if xp == XPperLevel(level) then
levelStore:Increment(1)
level = level + 1
plr.Level.Value = level
xpStore:Increment(-XPperLevel(level))
xp = xp - XPperLevel(level)
plr.XP.Value = xp
end
if levelold < level then
game.ServerStorage.UpdateOverhead:Fire(plr)
end
xpdebounces[plr] = false
return
end)
--My line to initiate this in the claim reward script
game.ServerStorage.AwardXP:Fire(player, game.ReplicatedStorage.ClientResources.Rewards['Day' .. (lastreward + 1)].XP.Value)
--A line that updates experience but also saves it
game.ServerStorage.AwardXP:Fire(players[i], game.ServerStorage.XPPerWin.Value)
In the end I’m just confused about what could cause the actual DataStore2 value to update for both of the functions, but then only remain upon login for one of the functions?
Edit: Here is the rest of my code in the server-side login reward manager
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
local debounces = {}
function claimReward(player)
if debounces[player] == true then return end
debounces[player] = true
local newtime = os.time()
if (newtime - game.Players[player.Name].lastreward.Value)/60/60 < 24 then
debounces[player] = false
return false
elseif (newtime - game.Players[player.Name].lastreward.Value)/60/60 > 24 then
local loginStore = DataStore2("dailylogin", player)
local lastrewardtime = loginStore:Get(0)
local rewardsStore = DataStore2("numrewardsclaimed", player)
local lastreward = rewardsStore:Get(0)
if newtime - lastrewardtime < 86400 then
game.Players[player.Name].lastreward.Value = lastrewardtime
debounces[player] = false
return false
elseif newtime - lastrewardtime > 86400 then
loginStore:Increment(newtime - lastrewardtime)
game.Players[player.Name].lastreward.Value = newtime
if lastreward == 4 then
game.ServerStorage.AwardCredits:Invoke(player, game.ReplicatedStorage.ClientResources.Rewards.Day5.Ice.Value)
game.ServerStorage.AwardXP:Fire(player, game.ReplicatedStorage.ClientResources.Rewards.Day5.XP.Value)
game.Players[player.Name].rewardsclaimed.Value = 0
rewardsStore:Increment(-4)
debounces[player] = false
return true
else
game.ServerStorage.AwardCredits:Invoke(player, game.ReplicatedStorage.ClientResources.Rewards['Day' .. (lastreward + 1)].Ice.Value)
---------ADDING THE EXPERIENCE FROM THE DAILY LOGIN BONUS HERE VVVV----------------
game.ServerStorage.AwardXP:Fire(player, game.ReplicatedStorage.ClientResources.Rewards['Day' .. (lastreward + 1)].XP.Value)
rewardsStore:Increment(1)
game.Players[player.Name].rewardsclaimed.Value = game.Players[player.Name].rewardsclaimed.Value + 1
debounces[player] = false
return true
end
end
end
end
game.ReplicatedStorage.RemoteFunctions.ClaimReward.OnServerInvoke = function(player)
claimReward(player)
end
Edit2: I tried changing the key, but that didn’t do anything. I also experimented some more and found that when the experience from the daily login bonus is added, it just appears as though it’s added. Everything runs as though it’s added. But then when more experience is added from other sources, it reverts back to what it was. The attached screenshot shows what the console looks like for the corresponding print statements. The first part is adding the daily login bonus experience, then adding other experience which adds to the old value because it didn’t actually update for the daily login or something.