Data Store Problem

Server:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Saves")
local saveRemote = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("Saving")
local mps = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(player)

	local ld = Instance.new("Folder", player)
	ld.Name = "leaderstats"

	local folder = Instance.new("Folder", player)
	folder.Name = "Data"

	local coins = Instance.new("IntValue", ld)
	coins.Name = "Coins"
	
	local bunkCoins = Instance.new("IntValue", folder)
	bunkCoins.Name = "BunkCoins"
	
	local upgradeClickAmount = Instance.new("IntValue", folder)
	upgradeClickAmount.Name = "UpgradeClickAmount"
	
	local clickPower = Instance.new("IntValue", folder)
	clickPower.Name = "ClickPower"

	local template = {
		[1] = 0,
		[2] = 0,
		[3] = 0,
		[4] = 0,
		[5] = 0,
		[6] = 0,
		[7] = 0,
	}

	local function Reconcile(data)
		for idx, val in template do
			if not data[idx] then data[idx] = val end
		end
	end

	local success, errormag = pcall(function()
		local getData = DataStore:GetAsync(player.UserId)
		for i, v in pairs(getData) do
			if i == 1 then
				coins.Value = getData[i]
			elseif i == 2 then
				bunkCoins.Value = getData[i]
			elseif i == 3 then
				upgradeClickAmount.Value = getData[i]
			elseif i == 4 then
				clickPower.Value = getData[i]
			end
		end
	end)

	if success then
		--print("Successfully loaded ".. player.Name.. "'s data!")
	elseif not success then
		-- FIRST TIME JOINING --
		coins.Value = 0
		bunkCoins.Value = 0
		upgradeClickAmount.Value = 10
		clickPower.Value = 1
	end
end)

saveRemote.OnServerEvent:Connect(function(player, val)
	DataStore:SetAsync(player.UserId, val)
	--print("Successfully saved ".. player.Name.. "'s data!")
end)

Local:

local player = game:GetService("Players").LocalPlayer
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("Saving")
local ld = game.Players.LocalPlayer:WaitForChild("leaderstats")
local folder = game.Players.LocalPlayer:WaitForChild("Data")

local savingTime = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("GameSettings"))

while true do
	wait(savingTime.gameSettings.savingCooldown) -- TIME FOR SAVE
	local saveDataTable = {}

	table.insert(saveDataTable, 1, ld.Coins.Value)
	table.insert(saveDataTable, 2, folder.BunkCoins.Value)
	table.insert(saveDataTable, 3, folder.UpgradeClickAmount.Value)
	table.insert(saveDataTable, 4, folder.ClickPower.Value)
	remote:FireServer(saveDataTable)
	--print("Autosaved ".. game.Players.LocalPlayer.Name.. "'s Data")
end

The problem is that when I create a new value, the first time I enter it, it is always 1, even though I would have written 1. I mean clickPower. What is the problem?

1 Like