DataStore refuses to save table values even after saving them individually on the server instead of in a Player instance

The currencies work, but anything past that breaks.

-- This for lazy people who just came to copy & paste, not to learn the code...

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("CreditsDataStore") -- This can be changed to whatever you want

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

	local tableToSave = {
		player.Currency.Credits.Value, -- 1
		player.SCurrency.SCredits.Value, -- 2
		workspace.PlayerDataPersist.PlayerData:WaitForChild(player.Name).MaxHP.Value, -- 3
		workspace.PlayerDataPersist.PlayerData:WaitForChild(player.Name).MaxHP.HP.Value, -- 4
		workspace.PlayerDataPersist.PlayerData:WaitForChild(player.Name).MaxHP.Shield.Value, -- 5
		workspace.PlayerDataPersist.leaderstats:WaitForChild(player.Name).Stage.Value, -- 6
		workspace.PlayerDataPersist.leaderstats:WaitForChild(player.Name).Mode.Value, -- 7
		player.Character:WaitForChild("GlobalCharacter").Value -- 8
	}

	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 currency = Instance.new("Folder")
	currency.Name = "Currency"
	currency.Parent = player

	local scurrency = Instance.new("Folder")
	scurrency.Name = "SCurrency"
	scurrency.Parent = player
	
	local playerData = Instance.new("Folder")
	playerData.Name = player.Name
	playerData.Parent = workspace.PlayerDataPersist.PlayerData
	
	local playerData2 = Instance.new("Folder")
	playerData2.Name = player.Name
	playerData2.Parent = workspace.PlayerDataPersist.leaderstats
	
	
	local Credits = Instance.new("IntValue")
	Credits.Name = "Credits"
	Credits.Parent = currency
	
	
	local SCredits = Instance.new("IntValue")
	SCredits.Name = "SCredits"
	SCredits.Parent = scurrency

	local max = Instance.new("IntValue")
	max.Name = "MaxHP"
	max.Value = 3
	max.Parent = playerData
	

	local hp = Instance.new("IntValue")
	hp.Name = "HP"
	hp.Value = 3
	hp.Parent = max
	

	local s = Instance.new("BoolValue")
	s.Name = "Shield"
	s.Value = false
	s.Parent = max

	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)
	print(unpack(data))
	if success and data then -- If there were no errors and player loaded the data
		
		Credits.Value = data[1] -- Set the money to the first value of the table (data)
		SCredits.Value = data[2]
		player.leaderstats.Mode.Value = data[7]
		player.PlayerGui.Preserve.Character.Value = data[8]
		local character = data[8]

		if character == "Sku" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Sku)
			player.Character:WaitForChild("SkuAbility").Loaded.Value = true

		elseif character == "Meme" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Meme)
			player.Character:WaitForChild("MemeAbility").Loaded.Value = true
		elseif character == "Phasec" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Phasec)
			player.Character:WaitForChild("PhasecAbility").Loaded.Value = true
		elseif character == "Tavo" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Tavo)
			player.Character:WaitForChild("TavoAbility").Loaded.Value = true
		elseif character == "Tay" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Tay)
			player.Character:WaitForChild("TayAbility").Loaded.Value = true
		elseif character == "TaintedDefault" then
			player:LoadCharacter()
			player.Character:WaitForChild("TDAbility").Loaded.Value = true
		elseif character == "TaintedTavo" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Tavo)
			player.Character:WaitForChild("TTAbility").Loaded.Value = true
		elseif character == "Alena" then
			player:LoadCharacterWithHumanoidDescription(workspace.CharacterHDS.Alena)
			player.Character:WaitForChild("AlenaAbility").Loaded.Value = true
		else

			player:LoadCharacter()
		end

		task.wait(0.5)
		player.Character.MaxHP.Value = data[3]
		player.Character.MaxHP.HP.Value = data[4]
		player.Character.MaxHP.Shield.Value = data[5]
		player.PlayerGui.Preserve.Id.Value = data[6]



	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)

Yes, it is a DevForum script, and I got it to work, but everything past those two values saves as nil for some weird reason. (obvious because of the top line… LOL)

Client side for health values:

game["Run Service"].RenderStepped:Connect(function()
	task.wait(1)
	workspace.SetPlayerData:FireServer(workspace.PlayerDataPersist.PlayerData:FindFirstChild(game.Players.LocalPlayer.Name).MaxHP.HP.Value, workspace.PlayerDataPersist.PlayerData:FindFirstChild(game.Players.LocalPlayer.Name).MaxHP.Value, workspace.PlayerDataPersist.PlayerData:FindFirstChild(game.Players.LocalPlayer.Name).MaxHP.Shield.Value)
end)

Server event:

script.Parent.OnServerEvent:Connect(function(player, dbHP, dbMaxHP, dbHasShield)
	local pData = workspace.PlayerDataPersist.PlayerData:FindFirstChild(player.Name)
	pData.MaxHP.Value = dbMaxHP
	pData.MaxHP.HP.Value = dbHP
	pData.MaxHP.Shield.Value = dbHasShield
end)

The game remains functional but I can’t save player data so they can resume at the point they were, I can only keep their credits. Is it because when you leave in a single player server everything gets deleted before you can save it correctly…?

1 Like

Able to fix it myself! Just had to change up how data was saved lol

1 Like

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