DataStore - Value Reset Without Error (With Video Now - Repost)

Hello devs, as a garbage scripter i wanted repost my problem with a video now because it wasn’t solved…

I have no error but idk why it reset Upgrade value after clicked upgrade again

I share scripts below.

Here video:
Click and watch problem on Youtube (It didn’t let me upload video here idk)

Upgrade Module in Replicated Storage:

local MotherModule = {}

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

return MotherModule

Local Script 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)

Did you change all of the values in server script?

Datastore code in severscript right now, the all codes i used already in the post.

You should get the value every time the mouse button is clicked, you should put that inside

1 Like

Oh my god bro. Is that all… I wasted one day for it T-T

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.