Datastorage ain't working?

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)
1 Like

That’s strange, data stores often don’t work well in studio. Your coins saving data runs only on the Player.Removing event, so it might not work as well in studio. Your daily reward on the other hand saves soon after they join, so there is much less chance of that not working which might be why that is working while your other script isn’t. Have you tried testing it in your actual game?

I’ve actually found a epic soloution to the DataStores not actually working when the player leaves the game and some data are getting saved! I’ve found out that when you just kick yourself out of the game using the Command Bar, the script that is made to save the data actually runs correctly. I’m not sure if I’m ok to put this to here, but I find it a cool thing and it could help others as well!

1 Like

I tried testing it and it still doesn’t work in-game.

I really really not recommend using Default Datastores, because it is inefficient and may lose data when the script does not run correctly, I would give you some DataModules like DataStore2 or ProfileService and those are very good modules for Data Handling

If that script is in server script, you cant get the local player in server script