Some datastore like deletes values. tbh i have no idea

I wanna fix the like datastore problem

I have no idea what the problem is since im getting no errors

i have just tried fixing myself but couldnt get it working

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Testing") -- Use "Testing" when in studio and "Published" when done

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Time.Value; -- First value from the table
		player.leaderstats.Level.Value; -- Second value from the table
		player.Stats.MoreTime.Value; -- Third value from the table
		math.round(player.Stats.BaseMoreTimeUpgradeCost.Value);
		math.round(player.Stats.BaseFasterTimeUpgradeCost.Value);
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

local Events = game.ReplicatedStorage.Events

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local PStats = Instance.new("Folder")
	PStats.Name = "Stats"
	PStats.Parent = player

	local Time = Instance.new("NumberValue")
	Time.Name = "Time"
	Time.Parent = leaderstats
	Time.Value = 0
	
	local level = Instance.new("NumberValue")
	level.Name = "Level"
	level.Parent = leaderstats
	level.Value = 0
	
	-- Other Stats
	
	local BaseMoreTimeUpgradeCost = Instance.new("NumberValue")
	BaseMoreTimeUpgradeCost.Name = "BaseMoreTimeUpgradeCost"
	BaseMoreTimeUpgradeCost.Parent = PStats
	BaseMoreTimeUpgradeCost.Value = 5
	
	local BaseFasterTimeUpgradeCost = Instance.new("NumberValue")
	BaseFasterTimeUpgradeCost.Name = "BaseFasterTimeUpgradeCost"
	BaseFasterTimeUpgradeCost.Parent = PStats
	BaseFasterTimeUpgradeCost.Value = 3
	
	local function RoundCosts()
		if BaseMoreTimeUpgradeCost.Value then
			BaseMoreTimeUpgradeCost.Value = math.round(BaseMoreTimeUpgradeCost.Value)
		end

		if BaseFasterTimeUpgradeCost.Value then
			BaseFasterTimeUpgradeCost.Value = math.round(BaseFasterTimeUpgradeCost.Value)
		end
	end
	


	local TimeTimer = Instance.new("NumberValue")
	TimeTimer.Name = "TimeTimer"
	TimeTimer.Parent = PStats
	TimeTimer.Value = 0
	
	local MoreTime = Instance.new("NumberValue")
	MoreTime.Name = "MoreTime"
	MoreTime.Parent = PStats
	MoreTime.Value = 1
	
	local TimeSpeed = Instance.new("NumberValue")
	TimeSpeed.Name = "TimeSpeed"
	TimeSpeed.Parent = PStats
	TimeSpeed.Value = 0.4
	
	local DEFAULT_TIME_TIMER_GAIN = 0.01
	local DEFAULT_TIME_TIMER_CLICK = 0.1
	local UpgradeWall = workspace.UpgradeWall
	
	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success and data then -- If there were no errors and player loaded the data

		if success and data then -- If there were no errors and player loaded the data
			Time.Value = data[1] or 0 -- Set the time to the first value of the table (data)
			level.Value = data[2] or 0 -- Set the level to the second value of the table (data)
			MoreTime.Value = data[3] or 0 -- Set the MoreTime to the third value of the table (data)

			-- Check if data[4] and data[5] are not nil before rounding them
			if data[4] then
				BaseMoreTimeUpgradeCost.Value = math.round(data[4])
			end

			if data[5] then
				BaseFasterTimeUpgradeCost.Value = math.round(data[5])
			end

	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end
	
	local debounce = false
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	player.PlayerGui:WaitForChild("SurfaceGui").MoreTimeUpgrade.TextButton.MouseButton1Click:Connect(function()
		
		if player.leaderstats.Time.Value >= BaseMoreTimeUpgradeCost.Value then
			
			Time.Value = Time.Value - BaseMoreTimeUpgradeCost.Value
			BaseMoreTimeUpgradeCost.Value = BaseMoreTimeUpgradeCost.Value ^ 1.1
			MoreTime.Value = MoreTime.Value + 1
			print("You Bought MoreTime Upgrade!")
		else
			print("Not Enought Time To Purchase")
		end
	end)
	
		player.PlayerGui:WaitForChild("SurfaceGui").FasterTimeUpgrade.TextButton.MouseButton1Click:Connect(function()
		
		if player.leaderstats.Time.Value >= BaseFasterTimeUpgradeCost.Value and TimeSpeed.Value >= 0.05 then
			
			Time.Value = Time.Value - BaseFasterTimeUpgradeCost.Value
			BaseFasterTimeUpgradeCost.Value = BaseFasterTimeUpgradeCost.Value ^ 1.3
			TimeSpeed.Value = TimeSpeed.Value - 0.05
			print("You Bought FasterTime Upgrade!")
		else
			print("Not Enought Time To Purchase")
		end
	end)
	
	workspace.TimePart.ClickDetector.MouseClick:Connect(function()
		print("Button Clicked")
		if debounce == false then
			TimeTimer.Value = TimeTimer.Value + DEFAULT_TIME_TIMER_CLICK
			debounce = true
			wait(0.1)
			debounce = false
		end
	end)

	spawn(function()
		while true do
			TimeTimer.Value = TimeTimer.Value + DEFAULT_TIME_TIMER_GAIN * (MoreTime.Value/4)
			wait(TimeSpeed.Value)

			if TimeTimer.Value >= 1 then
				TimeTimer.Value = 0
				Time.Value = Time.Value + 1
			end
		end
	end)
	
	spawn(function()
		while true do
			wait()
			RoundCosts()
		end
	end)
	
	
	

	
	end


game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
	end)
end)

This is my whole script. srry cuz its so long

1 Like

This could occur because you don’t verify if the player’s data has been loaded, which means that when you use SetAsync, you’re basically setting it to an empty array.

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Testing") -- Use "Testing" when in studio and "Published" when done

local function saveData(player) -- The functions that saves data
	local tableToSave = {
		player.leaderstats.Time.Value, -- First value from the table
		player.leaderstats.Level.Value, -- Second value from the table
		player.Stats.MoreTime.Value, -- Third value from the table
		math.round(player.Stats.BaseMoreTimeUpgradeCost.Value),
		math.round(player.Stats.BaseFasterTimeUpgradeCost.Value),
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)
	end
end

local Events = game.ReplicatedStorage.Events

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local PStats = Instance.new("Folder")
	PStats.Name = "Stats"
	PStats.Parent = player

	local Time = Instance.new("NumberValue")
	Time.Name = "Time"
	Time.Parent = leaderstats
	Time.Value = 0

	local level = Instance.new("NumberValue")
	level.Name = "Level"
	level.Parent = leaderstats
	level.Value = 0

	-- Other Stats

	local BaseMoreTimeUpgradeCost = Instance.new("NumberValue")
	BaseMoreTimeUpgradeCost.Name = "BaseMoreTimeUpgradeCost"
	BaseMoreTimeUpgradeCost.Parent = PStats
	BaseMoreTimeUpgradeCost.Value = 5

	local BaseFasterTimeUpgradeCost = Instance.new("NumberValue")
	BaseFasterTimeUpgradeCost.Name = "BaseFasterTimeUpgradeCost"
	BaseFasterTimeUpgradeCost.Parent = PStats
	BaseFasterTimeUpgradeCost.Value = 3

	local function RoundCosts()
		if BaseMoreTimeUpgradeCost.Value then
			BaseMoreTimeUpgradeCost.Value = math.round(BaseMoreTimeUpgradeCost.Value)
		end

		if BaseFasterTimeUpgradeCost.Value then
			BaseFasterTimeUpgradeCost.Value = math.round(BaseFasterTimeUpgradeCost.Value)
		end
	end

	local TimeTimer = Instance.new("NumberValue")
	TimeTimer.Name = "TimeTimer"
	TimeTimer.Parent = PStats
	TimeTimer.Value = 0

	local MoreTime = Instance.new("NumberValue")
	MoreTime.Name = "MoreTime"
	MoreTime.Parent = PStats
	MoreTime.Value = 1

	local TimeSpeed = Instance.new("NumberValue")
	TimeSpeed.Name = "TimeSpeed"
	TimeSpeed.Parent = PStats
	TimeSpeed.Value = 0.4

	local DEFAULT_TIME_TIMER_GAIN = 0.01
	local DEFAULT_TIME_TIMER_CLICK = 0.1
	local UpgradeWall = workspace.UpgradeWall

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
	end)

	if success and data then -- If there were no errors and player loaded the data
		if success and data then -- If there were no errors and player loaded the data
			Time.Value = data[1] or 0 -- Set the time to the first value of the table (data)
			level.Value = data[2] or 0 -- Set the level to the second value of the table (data)
			MoreTime.Value = data[3] or 0 -- Set the MoreTime to the third value of the table (data)

			-- Check if data[4] and data[5] are not nil before rounding them
			if data[4] then
				BaseMoreTimeUpgradeCost.Value = math.round(data[4])
			end

			if data[5] then
				BaseFasterTimeUpgradeCost.Value = math.round(data[5])
			end
		else -- The player didn't load in the data, and probably is a new player
			print("The player has no data!") -- The default will be set to 0
		end

		local debounce = false

		player.PlayerGui:WaitForChild("SurfaceGui").MoreTimeUpgrade.TextButton.MouseButton1Click:Connect(function()
			if player.leaderstats.Time.Value >= BaseMoreTimeUpgradeCost.Value then
				Time.Value = Time.Value - BaseMoreTimeUpgradeCost.Value
				BaseMoreTimeUpgradeCost.Value = BaseMoreTimeUpgradeCost.Value ^ 1.1
				MoreTime.Value = MoreTime.Value + 1
				print("You Bought MoreTime Upgrade!")
			else
				print("Not Enought Time To Purchase")
			end
		end)

		player.PlayerGui:WaitForChild("SurfaceGui").FasterTimeUpgrade.TextButton.MouseButton1Click:Connect(function()
			if player.leaderstats.Time.Value >= BaseFasterTimeUpgradeCost.Value and TimeSpeed.Value >= 0.05 then
				Time.Value = Time.Value - BaseFasterTimeUpgradeCost.Value
				BaseFasterTimeUpgradeCost.Value = BaseFasterTimeUpgradeCost.Value ^ 1.3
				TimeSpeed.Value = TimeSpeed.Value - 0.05
				print("You Bought FasterTime Upgrade!")
			else
				print("Not Enought Time To Purchase")
			end
		end)

		workspace.TimePart.ClickDetector.MouseClick:Connect(function()
			print("Button Clicked")
			if debounce == false then
				TimeTimer.Value = TimeTimer.Value + DEFAULT_TIME_TIMER_CLICK
				debounce = true
				wait(0.1)
				debounce = false
			end
		end)

		spawn(function()
			while true do
				TimeTimer.Value = TimeTimer.Value + DEFAULT_TIME_TIMER_GAIN * (MoreTime.Value / 4)
				wait(TimeSpeed.Value)

				if TimeTimer.Value >= 1 then
					TimeTimer.Value = 0
					Time.Value = Time.Value + 1
				end
			end
		end)

		spawn(function()
			while true do
				wait()
				RoundCosts()
			end
		end)

        local FullyLoaded = Instance.new("BoolValue")
		FullyLoaded.Name = "FullyLoaded"
		FullyLoaded.Value = true
		FullyLoaded.Parent = player
		warn(`Loaded {player}'s data`)
	end

	game:BindToClose(function() -- When the server shuts down
		for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
			local success, err = pcall(function()
				saveData(player) -- Save the data
			end)

			if success then
				print("Data has been saved")
			else
				print("Data has not been saved!")
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
    player:WaitForChild("FullyLoaded")
    local success, err = pcall(function()
        saveData(player) -- Save the data
    end)

    if success then
        print("Data has been saved")
    else
        print("Data has not been saved!")
    end
end)
1 Like

It fixed a bit of the data but 2 of them specifecly are still broken.

BaseFasterTimeUpgradeCost and BaseMoreTimeUpgradeCost

BaseFasterTimeUpgradeCost has taken the value of BaseMoreTimeUpgradeCost
And BaseMoreTimeUpgradeCost value has been set to 0. i dont rlly understand why

idk if im dumb or smth but its the same problem.

BaseFasterTimeUpgradeCost has taken the value of BaseMoreTimeUpgradeCost
And BaseMoreTimeUpgradeCost value has been set to 0

This may be a problem with your code’s logic itself. Are you sure it isn’t?

Put the “game:BindToClose” and “PlayerRemoving” events outside of the PlayerAdded event and it will work :wink:

yeah that was just dumb by me but im still having the same issue. were the values are getting wrong. But just on those 2 values. i swear, its prob smth dumb.

to be honest i have no idea. Im a new dev and struggeling a bit

1 Like