DataStore doesnt save

Yes I’m aware I posted previously about this but it seems I ran in to another bug.

I was looking at the wiki and did a lot based off what they said and suggested… So why wouldn’t this save?

local UserData = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local Framework = ReplicatedStorage:WaitForChild("Framework")
local Modules = Framework:WaitForChild("Modules")
local Houses = require(Modules.Utilities:WaitForChild("Houses"))
local DataStore = game:GetService("DataStoreService"):GetDataStore("Data")
local Utilities = Modules:WaitForChild("Utilities")
local GroupUtility = require(Utilities:WaitForChild("GroupUtility"))
local AddObjects = require(Utilities:WaitForChild("AddObjects"))
local DataStoreFailures = require(Utilities:WaitForChild("DataStoreFailures"))
--local ArrestModule = require(Modules:WaitForChild("ArrestModule"))
local serverData = {}
local DefaultData = { -- Datastore including the users Currency, House, House Rank, and Profile information
	['House'] = {
		['Name'] = nil; -- Resulting to NIL. This isn't needed but is good for organization. 
		['Rank'] = nil;
		['HouseIcon'] = nil;
		['HouseID'] = nil;
	};
	['MainGroupRank'] = nil;
	['Currency'] = 0;
	['ProfileDescription'] = "Default Profile Description!";
	['ArrestData'] = {
		['Reason'] = "None";
		['Time'] = 0;
	};
};

function UserData:ChangeStat(player,statName,newValue) -- Updating a users value
	if statName == "ArrestTime" then
		serverData[player].ArrestData.Time = newValue
	elseif statName == "ArrestReason" then
		serverData[player].ArrestData.Reason = newValue
	end
end

local function getPlayerData(player)
	return DataStoreFailures:dataStoreRetry(function() 
		return playerData:GetAsync("USR6_"..player.UserId)
	end)
end

function UserData:savePlayerData(player)
	if serverData[player] then
		return DataStoreFailures:dataStoreRetry(function()
			return playerData:SetAsync("USR6_"..player.UserId, serverData[player])
		end)
	end
end

local function GetHouseName(plr) -- Called during the load data process
	for index,house in pairs(Houses) do
		--local CacheHouseInformation = GroupUtility:PlayerInGroup(plr,house.GroupID)
		if plr:IsInGroup(house.GroupID) == true then
			return house.Name,house.GroupID,house.Icon -- Id is the group id, would still need to return names, etc.. (6/24/18, CHANGED TO NAME)
		end
	end
	return "Houseless"
end

local function loadObjects(plr) -- To clean the code up this was put in a function during V2
	serverData[plr] = DefaultData
	AddObjects:Folder("Data",plr)
	local DataFolder = plr:FindFirstChild("Data")
	local HouseArgs = {GetHouseName(plr)}
	local UsrData = serverData[plr]
	UsrData.House.Name = HouseArgs[1]
	if UsrData.House.Name ~= "Houseless" then
		UsrData.House.Rank = GroupUtility:GetRoleInGroup(plr,HouseArgs[2])
		UsrData.House.HouseID = HouseArgs[2]
	else
		UsrData.House.Rank = "Sorry! This user isn't in a house."
	end
	if HouseArgs[3] ~= nil then
		UsrData.House.HouseIcon = "rbxassetid://"..HouseArgs[3]
	else
		UsrData.House.HouseIcon = "rbxassetid://1110396322"
	end
	if UsrData.House.HouseID == nil then
		UsrData.House.HouseID = 1
	end
	print(UsrData.ArrestData.Time)
	UsrData.MainGroupRank = GroupUtility:GetRoleInGroup(plr,4141896)
	AddObjects:NumberValue("HouseID",DataFolder,UsrData.House.HouseID)
	AddObjects:StringValue("HouseIcon",DataFolder,UsrData.House.HouseIcon)
	AddObjects:StringValue("MainGroupRank",DataFolder,UsrData.MainGroupRank)
	AddObjects:StringValue("House",DataFolder,UsrData.House.Name)
	AddObjects:StringValue("HouseRank",DataFolder,UsrData.House.Rank)
	AddObjects:NumberValue("Currency",DataFolder,UsrData.Currency)
	AddObjects:StringValue("ProfileDescription",DataFolder,UsrData.ProfileDescription)
	AddObjects:NumberValue("ArrestTime",DataFolder,UsrData.ArrestData.Time)
	AddObjects:StringValue("ArrestReason",DataFolder,UsrData.ArrestData.Reason)
end

local function setupPlayerData(player)
	local success, data = getPlayerData(player)
	if not success then
		serverData[player] = false
	else
		if not data then
			loadObjects(player)
			UserData:savePlayerData(player)
		else
			loadObjects(player)
			serverData[player] = data
		end
	end	
end

local function autosave()
	while wait(60) do
		for player, data in pairs(serverData) do
			UserData:savePlayerData(player)
		end
	end
end

game.Players.PlayerAdded:connect(setupPlayerData)

spawn(autosave)

return UserData
local function getPlayerData(player)
	local success = false
	local success = DataStoreFailures:dataStoreRetry(function() 
		if success then
			return true,playerData:GetAsync("USR6_"..player.UserId)
		end
	end)
end

function UserData:savePlayerData(player)
	if serverData[player] then
		local success = false
		success = DataStoreFailures:dataStoreRetry(function()
			if success then
				return true,playerData:SetAsync("USR6_"..player.UserId, serverData[player])
			end
		end)
	end
end

That should fix your problem. Hope that helps! :slight_smile:

You should try using print()

&

Is there any Errors?

No errors, even when debugging everything seems to work.

I’ve done prints and everything works… besides saving

Have you tried the code I supplied above?

You removed it…?

I never saw the code just your suggestion about printing

I removed the message asking to add print lines, lol. My very first reply is what I’m referring to.

Now none of the data loads at all…

This probably won’t fix your issues but you should be saving data when a player leaves and suspending the server during shutdown to do so.

LOL Hmph… Let me take a further look. Although it’s somewhat lacking of information as much of the script runs off other modules, so it makes it harder to test on my end. It worked earlier when I tested it, but I didn’t have any of the modules.

Oh, I just realized that I had 2 local success variables in the getPlayerData. Try this?

local function getPlayerData(player)
	local success = false
	success = DataStoreFailures:dataStoreRetry(function() 
		if success then
			return true,playerData:GetAsync("USR6_"..player.UserId)
		end
	end)
end

That function is called when the player leaves…

@xXLegendarySoldierXx er… Cool?

still in need to figure out why this is happening. Any ideas?