Saving a Table of IntValues in Data Store isn't Working

Problem
I can’t save a table of intvalues, or my logic to fire remote events is wrong.
What I’ve Tried
Using DataStore to keep the intvalue table saved. I reused code to save a single intvalue.
(I think my remote events aren’t firing but I’ll make another topic for that)

Code

local Upgrade1 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave1
local Upgrade2 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave2
local Upgrade3 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave3
local Upgrade4 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave4
local Upgrade5 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave5
local Upgrade6 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave6
local Upgrade7 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave7
local Upgrade8 = game.ReplicatedStorage.ClassFolder.ClassUpgrades.Juggernaut.UpgradeSave8

local classTable = {Upgrade1.Value, Upgrade2.Value, Upgrade3.Value, Upgrade4.Value, Upgrade5.Value, Upgrade6.Value, Upgrade7.Value, Upgrade8.Value}
--Data Store
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")
local classStore = DataStoreService:GetDataStore("JuggernautStore")

local players = game:GetService("Players")

local function onShutDown()
	task.wait(3)
end

local function setUp(player)
	local userID = player.UserId
	local key = "Player_"..userID

	local data = classStore:GetAsync(key)
	local success, ret = pcall(classStore.GetAsync, classStore, key)

	if success then
		classTable = ret or 0
	else
		print("Uh oh there's an error man "..ret)
	end

	repeat
		local success, ret = pcall(classStore.GetAsync, classStore, key)
	until success or not players:FindFirstChild(players.Name)

	classTable = data or 0
end

local function save(player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	wait(6)
	classStore:SetAsync(key, classTable)

	local success, ret = pcall(classStore.SetAsync, classStore, key)

	if success then
		print("Yippee! The Upgrade have been SAVED")
	else
		print("Uh oh there's an error man "..ret)
	end

	repeat
		local success, ret = pcall(classStore.SetAsync, classStore, key)
	until success or not players:FindFirstChild(players.Name)
end

while wait(60) do
	players.PlayerRemoving:Connect(save)
end

game:BindToClose(onShutDown)
players.PlayerAdded:Connect(setUp)
players.PlayerRemoving:Connect(save)

the table you defined in the beginning with all of the values will not update when an IntValue changes values. that pretty much copies down what their values all are when the script starts, then never changes them

and a common issue you might have after fixing that is updating IntValue values on clientside. make sure you arent going into the explorer and changing the values of leaderstats without changing to serverside in the Test tab of studio. and then make sure you arent using local scripts to update their values either

I actually forgot that changing values on clientside doesn’t change it for the server :man_facepalming:
Thank you for reminding me

1 Like

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