DataStore Suddenly not working properly!

This script was working completely before. I don’t know what happened but both the cash value and oil value seemed to have not saved my old value instead it saved only the new value. (Right now its working correctly)

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderStats = Instance.new("Folder",Player)
	leaderStats.Name = "Leaderstats"
	
	local cash = Instance.new("NumberValue", leaderStats)
	cash.Name = "Cash"	
	cash.Value = 0
	
	local Oil = Instance.new("NumberValue", leaderStats)
	Oil.Name = "Oil"
	Oil.Value = 0
	
	local OwnsTycoon = Instance.new("BoolValue", Player)
	OwnsTycoon.Name = "OwnsTycoon"
	OwnsTycoon.Value = false
	
	local data1 = myDataStore:GetAsync(Player.UserId.."-cash")
	local data2 = myDataStore:GetAsync(Player.UserId.."-oil")
	local Success, Errormessage = pcall(function() 
		data1 = myDataStore:GetAsync(Player.UserId.."-cash")
		data2 = myDataStore:GetAsync(Player.UserId.."-oil")
		
	end)
	
	if Success then
		
		cash.Value = data1
		Oil.Value = data2

	else
		print("Error occured when trying to load data")
		warn(Errormessage)
	end
end)

--DataSaving--
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormesage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash", player.Leaderstats.Cash.Value)
		myDataStore:SetAsync(player.UserId.."-oil", player.Leaderstats.Oil.Value)
	end)
	
	if success then
		print("Data Saved Successfully")
	else
		print("An error occured when trying to save data")
		warn(errormesage)
	end
end)


.

Can you tell us the error message?

1 Like

no errors surprisingly
(i need more characters)

Try using UpdateASync.

1 Like