DataStore2 not Saving or Loading

This was working before and I’m not sure what caused it to break… It’s supposed to work so that each time a new player joins, a random name is generated using User.new(), and that is the default value of GetTable. It stopped working and now every time I rejoin I am given a random character, which doesn’t save. No errors are occurring.

This should hopefully identify whatever the problem is:

local defaultUserData = function(playerid)
local user = User.new(playerid)
return user
end

function OnPlayerAdded(player)
	local defaultUser = defaultUserData(player.UserId)
	local PlayerDataStore = DataStore2(DATASTORE_VERSION, player)
	local PlayerData = PlayerDataStore:GetTable(defaultUser.data)
	PlayerDataStore:Set(PlayerData) -- Saves the default data if not already initiated
	PlayerDataHandler.AddPlayer(player.UserId, PlayerData) -- must go before fireclient
	UpdateClientData:FireClient(player, PlayerData) -- Earlier error was firing client before the player was added, resulting in nil value
	setUpCharacter(PlayerData)
	player.CharacterAdded:Connect(function() -- sets up character every respawn
		setUpCharacter(PlayerData)
	end)
end

is there a reason why your calling this function twice?? setUpCharacter(PlayerData) shouldnt it only be called when the character is added.

That’s not the problem, but yes because characterAdded wasn’t firing when players initially joined the game, only after dying and re-spawning, so I decided to just run setUpCharacter once and then listen for respawns

can i see how you save the data when the player is removed?

Well it was supposed to be saving automatically because it’s datastores2, but I added this in an attempt to fix the no-save problem:

function OnPlayerRemoving(player)
local PlayerDataStore = DataStore2(DATASTORE_VERSION, player)
PlayerDataStore:Set(PlayerDataHandler.GetData(player.UserId))
PlayerDataHandler.RemovePlayer(player.UserId)
-- no need to save using datastores2, update datastores as the values update
-- playerdatahandler.removeplayer
end

Also I put a test for problems with the default value in the datastore2 GetTable function, and nothing is printing so it’s not a nil key problem:

for defaultKey, defaultValue in pairs(default) do
		if result[defaultKey] == nil then
			print("defaultkeynil")
			result[defaultKey] = defaultValue
			changed = true
		end
	end

oh your using the datastore2 module i am not familiar with this model of datastoring yet but ill check it out now and see if i can help

1 Like

Could it be that datastore2 is somehow overloaded and not saving the data in time before the server shuts down? I’ve been using it pretty aggressively and shutting down servers only seconds after joining and leaving

most likely not Alot of big games use this module fine, like dungeon quest

Yeah it’s not a problem with shutting down, I was just in a server for about 15 minutes and left, rejoined, and it still gave me a new random user.

Somehow every time datastore2 tries to load a players data, it is returning User.new(), as if it’s ALWAYS using the default value. The problem is, it’s not using the defaultvalue because this isn’t printing anything which would indicate that it is:

print("defaultkeynil")
result[defaultKey] = defaultValue
changed = true

I’m totally lost – the keys in User.new() are always the same, even though the values are different. :Set() is being called, and defaultkeynil never prints.

Here’s the User module in case it’s any help:

local UpdateClientData = game:GetService("ReplicatedStorage").RemoteEvents.UpdateClientData
local Players = game:GetService("Players")

local RandomName = require(game:GetService("ServerScriptService").RandomnessModules.RandomName)
local RandomSkinTone = require(game:GetService("ServerScriptService").RandomnessModules.RandomSkinTone)

local User = {}

User.__index = User

User.new = function(playerid)
	
	local randomGender = math.random(1,2)
	local randomSkinTone = RandomSkinTone.new()
	local gender = nil
	
	if randomGender == 1 then
		gender = "MALE"
	else
		gender = "FEMALE"
	end
	
	local firstName, middleName, lastName = RandomName.new(gender)
	local dataTable = {
			["USERID"] = playerid,
			["LIVES"] = 3,
			["FIRST_NAME"] = firstName,
			["MIDDLE_NAME"] = middleName,
			["LAST_NAME"] = lastName,
			["DAY"] = 0,
			["CASH"] = 0,
			["GENDER"] = gender,
			["SKIN_TONE"] = randomSkinTone
	}
	
	return setmetatable({
		playerid = playerid,
		data = dataTable
	}, User)
end

function User:GetValue(key)
	return self.data[key]
end

function User:SetValue(key, value)
	self.data[key] = value
	-- all data changes MUST go through here, do not modify player data in any other way
	
	-- fire updateclientdata so the client has accurate data information after changes
	-- save data in datastore2
	UpdateClientData:FireClient(Players:GetPlayerByUserId(self.playerid), self.data) -- sends User.data as argument
end

return User

Ok, so I figured out the cause of the problem,

I messed around with the User.data, and I removed SKIN_TONE, and it began working again.

The problem is clearly that I stored Color3 values in a table I tried to save, so I guess I can’t do that? Is there a way to save color3 values or do I just need to save R G B individually?? That’s odd though because I was under the impression you could save tables…

1 Like

use tostring() to save it and tonumber() or color3.new() to load it