DataStore IntValue reset itself without error. [HELP]

Hello Devs, i have so weird problem. I hope i can explain it.

I have a dataStore, Module and Upgrade script. I will give all scripts below text.

Module has upgrade prices as a tables. like first upgrade 100$ second 250$…

When you click imagebutton you earn 1 Strength. And when you make 100 Stregth you go to upgrade place and click upgrade image button and it delete 100 Strength.

Then you click again for save 250 strength. While you clicking upgrade button, for every upgrades the IntValue in Player > sheets folder > Upgrade.Value gets +1

So it changes Module.upgradePrices[UpgradeVal+1]

So everything works perfectly, it saves strenght and your current upgrade value.

The problem is here

  • I joined game,
  • I have 400 Strenght.
  • I upgraded 2 times for 100st and 250st and now i have 400-350 = 50 Strenght and Upgrade Value 2
  • I left game and rejoined.
  • I still have 2 Upgrade value and 50 Streght
  • I started clicking for earn some st. and got 150 strenght and clicked upgrade
  • It didn’t upgrade because the next upgrade is 350 Streght
  • So i have 150 Streght now.
  • I left game and joined back
  • I have still 150 Strenght and 2 Upgrade Value
  • Now i clicked upgrade again and it upgraded it. It deleted 100 and give you 1 value again.

It shouldn’t upgrade here… It doesn’t make sense.

I hope someone will understand me…

Module in ReplicatedStorage:

local MotherModule = {}

MotherModule.upgradePrices = {
	100,
	250,
	350,
	500,
	750,
	1000
}
return MotherModule

LocalScript in StarterGui

local ScreenGui = Instance.new("ScreenGui")
local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local Module = require(game.ReplicatedStorage.Upgrade)
local Upgrade = Module.upgradePrices
local upgradeButton = script.Parent.Upgrade.Frame.Upgrade

ScreenGui.Enabled = true
ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")


local TextButton = Instance.new("TextButton")
TextButton.Size = UDim2.new(0, 200, 0, 50)
TextButton.Position = UDim2.new(0.5, -100, 0.5, -25)
TextButton.Text = "Strength +1"
TextButton.Parent = ScreenGui

TextButton.MouseButton1Click:Connect(function()
	local localplayer = game.Players.LocalPlayer
	player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
	local newValue = player.leaderstats.Strength.Value
	RS.UpdateValue:FireServer(newValue, localplayer)
end)

local UpgradeVal = player.sheets.Upgrade.Value

upgradeButton.MouseButton1Click:Connect(function()
	
	local currentUpgrade = Module.upgradePrices[UpgradeVal+1]
	local localplayer = game.Players.LocalPlayer
	
	if player.leaderstats.Strength.Value >= currentUpgrade then
		player.leaderstats.Strength.Value -= currentUpgrade
		UpgradeVal = UpgradeVal + 1
		local upvalue = UpgradeVal
		RS.UpgradeVal:FireServer(upvalue,localplayer)
		local newValue = player.leaderstats.Strength.Value
		RS.UpdateValue:FireServer(newValue, localplayer)
	else
		print("Not enough Strenght")
	end
	
end)

DataStore:

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore1") -- This can be changed to whatever you want
local RS = game:GetService("ReplicatedStorage")
local updateValue = RS:FindFirstChild("UpdateValue")
local updateVal = RS:FindFirstChild("UpgradeVal")

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

	local tableToSave = {
		player.leaderstats.Strength.Value; -- First value from the table
		player.leaderstats.Wins.Value; -- Second value from the table
		player.sheets.Upgrade.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

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game

	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Sheets = Instance.new("Folder")
	Sheets.Name = "sheets"
	Sheets.Parent = player

	local Strength = Instance.new("IntValue")
	Strength.Name = "Strength"
	Strength.Parent = leaderstats

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Upgrade = Instance.new("IntValue")
	Upgrade.Name = "Upgrade"
	Upgrade.Parent = Sheets

	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
		
	local function values(playerC,newValue)
		if player == playerC then
				Strength.Value = newValue
		end
	end

		updateValue.OnServerEvent:Connect(values)
		
	local function upgrade(playerC,upvalue)
		if player == playerC then
				Upgrade.Value = upvalue
		end
	end

		updateVal.OnServerEvent:Connect(upgrade)	
	end)

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

		Strength.Value = data[1] -- Set the money to the first value of the table (data)
		Wins.Value = data[2]
		Upgrade.Value = data[3]-- Set the coins to the second value of the table (data)

	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

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)

Here,i will give you updated versions of your scripts.

Are you shutting down studio with Shift + F5? or Leaving as you do in roblox?

Local:

local ScreenGui = Instance.new("ScreenGui")
local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local Module = require(game.ReplicatedStorage.Upgrade)
local Upgrade = Module.upgradePrices
local upgradeButton = script.Parent.Upgrade.Frame.Upgrade

ScreenGui.Enabled = true
ScreenGui.Parent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")


local TextButton = Instance.new("TextButton")
TextButton.Size = UDim2.new(0, 200, 0, 50)
TextButton.Position = UDim2.new(0.5, -100, 0.5, -25)
TextButton.Text = "Strength +1"
TextButton.Parent = ScreenGui

TextButton.MouseButton1Click:Connect(function()
	local localplayer = game:GetService("Players").LocalPlayer
	player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
	local newValue = player.leaderstats.Strength.Value --Hey you made this line not protectable.Add the newvalue on server.if a client exploiter change the Strength value they can have unlimited strength!
	RS.UpdateValue:FireServer(newValue, localplayer)
end)

local UpgradeVal = player.sheets.Upgrade.Value

upgradeButton.MouseButton1Click:Connect(function()
	local localplayer = game:GetService("Players").LocalPlayer
	
	if player.leaderstats.Strength.Value >= currentUpgrade then
                local currentUpgrade = Module.upgradePrices[UpgradeVal+1]
		player.leaderstats.Strength.Value -= currentUpgrade
		UpgradeVal = UpgradeVal + 1
		local upvalue = UpgradeVal
		RS.UpgradeVal:FireServer(upvalue,localplayer)
		local newValue = player.leaderstats.Strength.Value
		RS.UpdateValue:FireServer(newValue, localplayer)
	else
		print("Not enough Strenght")
	end
	
end)

DataStore:

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore1") -- This can be changed to whatever you want
local RS = game:GetService("ReplicatedStorage")
local updateValue = RS:FindFirstChild("UpdateValue")
local updateVal = RS:FindFirstChild("UpgradeVal")

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

	local tableToSave = {
		player.leaderstats.Strength.Value; -- First value from the table
		player.leaderstats.Wins.Value; -- Second value from the table
		player.sheets.Upgrade.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

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game

	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Sheets = Instance.new("Folder")
	Sheets.Name = "sheets"
	Sheets.Parent = player

	local Strength = Instance.new("IntValue")
	Strength.Name = "Strength"
	Strength.Parent = leaderstats

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Upgrade = Instance.new("IntValue")
	Upgrade.Name = "Upgrade"
	Upgrade.Parent = Sheets

	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
		
	local function values(playerC,newValue)
		if player == playerC then
				Strength.Value = newValue
		end
	end

		updateValue.OnServerEvent:Connect(values)
		
	local function upgrade(playerC,upvalue)
		if player == playerC then
				Upgrade.Value = upvalue
		end
	end

		updateVal.OnServerEvent:Connect(upgrade)	
	end)

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

		Strength.Value = data[1] -- Set the money to the first value of the table (data)
		Wins.Value = data[2]
		Upgrade.Value = data[3]-- Set the coins to the second value of the table (data)

	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

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 ipairs(game:GetService("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)

try these ones.if it wont work just call me again!
Warning you⚠️: You are updating the value on the CLIENT and SERVER updates it with the value CLIENT gave.This is unprotectable.if someobody changes the strength like 9999,when they press the strength button they will have 10000 strenght.Instead of doing this,send a remote to the SERVER.and make the SERVER add the value so you may have a protectable game :smiley:

Hey bro, thank you for helping and warning. But nothing changed? And i leave like game.

Dta store will save everything

It working now with my old script. But i didn’t even change something. It all same with my posted scripts. Why it working now? I don’t understand.

Problem fixed itself, i have no clue. I did not change anything

Now it broken again… What’s happening. Why it fix and broke randomly

Only reson is that datastore has too many things to load,resulting in some of them being reseted

What should i do then, it so weird

And also repeat the success a few timescause it might not save all the data

I am not sure how to do it, when it takes so long it says data in queue and sending fewer requests. and save it in the end.

Local attempts = 5

Repeat
Wait(.3)
Local succes… your code for the success thingy

Until attempts == 0

Do that on the saveData(plauer)

Oh,a big error that shows that it is not saved fully

1 Like

Did it work???The main thing is it have to work

Ah, i was busy with something else. Let me try it now bro.

Is it like this? Because it just spammed:
**DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = **

Code:

local function saveData(player) -- The functions that saves data
	local attempts = 5
	local tableToSave = {
		player.leaderstats.Strength.Value; -- First value from the table
		player.leaderstats.Wins.Value; -- Second value from the table
		player.sheets.Upgrade.Value
	}	
	repeat
		wait(0.3)
	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
	until attempts == 0
end

Put the value in another folder into the leaderstats folder

Bro if i put the value in the leaderstats folder, it will be shown on leaderboard. I don’t want to show Upgrade Value on Leaderstats.

back after 2 days.what issue you are having now?

1 Like