Module Datastore System Not Setting Instanced Values?

Thanks to a fellow developer forum, I was able to sort out an annoying issue where the table which stores all the data appeared to be nil. Now, I’m running into another issue.

Despite me attempting to quickly set the number/string values to the retrieved values stored within keys of a table, it still keeps all values at either:

  1. 0

or

  1. Nil

It confuses me, as to why this is the case, although I do question my logic in this question. Maybe I’m implementing it wrong? I don’t know, but for you to understand what I’m attempting to implement, let me explain it in more detail.

So I have a module named userData, which handles all userdata, saving, loading, updating etc. This is the core to all key functions within my game.

How it works is, whenever a player joins, a table is created (saved as a key) under the overall module script table and that table is allocated to the player to manage data.

module["PlayerData-"..tostring(Player.UserId)] = {
		["SpleefCoins"] = 0,
		["CRW"] = false,
		["Wins"] = 0,
		["Benefits"] = 1,
		["Equipped"] = "1",
		["EquippedEffect"] = "1",
		["Level"] = "1",
		["Rank"] = "Newbie",
		["XP"] = 0,
		["XP_Limit"] = 30
	}
	module["Inventory-"..tostring(Player.UserId)] = {
		["Trails"] = {
			"1"
		},
		["Effects"] = {
			"1"
		}
	}

What I’m attempting to do is within my joinHandler script, make it so it loops through the table of all value instances, checking if the value’s name is equal to any of the keys’ names within here:

	["SpleefCoins"] = 0,
		["CRW"] = false,
		["Wins"] = 0,
		["Benefits"] = 1,
		["Equipped"] = "1",
		["EquippedEffect"] = "1",
		["Level"] = "1",
		["Rank"] = "Newbie",
		["XP"] = 0,
		["XP_Limit"] = 30

I want it to automatically set the values to these ^ but it doesn’t appear to work.

Help would be very much appreciated:

userData Module:

local module = {}
---This Module Handles Players Joining, Players Leaving and Backups
local DatastoreService = game:GetService("DataStoreService")
local PlayerDatastore = DatastoreService:GetDataStore("PDatastore_1")
local InventoryDatastore = DatastoreService:GetDataStore("InvStore_1")
local BackupPlayerDatastore = DatastoreService:GetDataStore("BackupPDataDatastore")
local BackupInventoryStore = DatastoreService:GetDataStore("BackupInvStore")
function module.AddPlayerData(Player)
	module["PlayerData-"..tostring(Player.UserId)] = {
		["SpleefCoins"] = 0,
		["CRW"] = false,
		["Wins"] = 0,
		["Benefits"] = 1,
		["Equipped"] = "1",
		["EquippedEffect"] = "1",
		["Level"] = "1",
		["Rank"] = "Newbie",
		["XP"] = 0,
		["XP_Limit"] = 30
	}
	module["Inventory-"..tostring(Player.UserId)] = {
		["Trails"] = {
			"1"
		},
		["Effects"] = {
			"1"
		}
	}
	print("Added Preset Data For PlayerData-"..tostring(Player.UserId))
end


function module.LoadInData(Player)
	local PlayerUserId = Player.UserId
	local PlayerKey = "Player-"..PlayerUserId
	local PlayerData, InventoryData
	local success, errormessage = pcall(function()
		PlayerData = PlayerDatastore:GetAsync(PlayerKey)
		InventoryData = InventoryDatastore:GetAsync(PlayerKey)
	end)
	if success then
		if PlayerData ~= nil then
			module["PlayerData-"..PlayerUserId] = PlayerData
		end
		if InventoryData ~= nil then
			module["Inventory-"..PlayerUserId] = InventoryData
		end
		print("All Data Fetched Successfully!")

	else
		warn("This Error Occured While Fetching The Data: "..errormessage)		
	end
end


function module.UpdatePlayerData(Player, oldValueName, newValue)
	local PlayerTable = module["PlayerData-"..Player.UserId]
	for indexValue, Value in pairs(PlayerTable) do
		if indexValue == oldValueName then
			indexValue = newValue
		end
	end
end

function module.UpdateInventoryData(Player, oldValueName, newValue)
	local InventoryTable = module["Inventory-"..Player.UserId]
	for indexValue, Value in pairs(InventoryTable) do
		if indexValue ==  oldValueName then
			indexValue = newValue
		end
	end
end

function module.SaveAllData()
	local PlayerUserId 
	local PlayerKey
	for indexPlayer, Player in pairs(game.Players:GetPlayers()) do
		PlayerUserId = Player.UserId
		PlayerKey = "Player-"..PlayerUserId
		local PlayerTable = module["PlayerData-"..tostring(PlayerUserId)]
		local InventoryTable = module["Inventory-"..PlayerUserId]
		if PlayerTable then
			local success, errormessage = pcall(function()
				PlayerDatastore:SetAsync(PlayerKey, PlayerTable)
				BackupPlayerDatastore:SetAsync(PlayerKey, PlayerTable)
				InventoryDatastore:SetAsync(PlayerKey, InventoryTable)
				BackupInventoryStore:SetAsync(PlayerKey, InventoryTable)
			end)
			if success then
				print("Saved "..Player.Name.."'s Data Successfully")
			else
				wait("Error: "..errormessage)
			end
		end
	end
end

function module.SaveData(Player)
	local PlayerUserId = Player.UserId
	PlayerKey = "Player-"..PlayerUserId
	local PlayerTable = module["PlayerData-"..tostring(PlayerUserId)]
	local InventoryTable = module["Inventory-"..PlayerUserId]
	if PlayerTable then
		local success, errormessage = pcall(function()
			PlayerDatastore:SetAsync(PlayerKey, PlayerTable)
			BackupPlayerDatastore:SetAsync(PlayerKey, PlayerTable)
			InventoryDatastore:SetAsync(PlayerKey, InventoryTable)
			BackupInventoryStore:SetAsync(PlayerKey, InventoryTable)
		end)
		if success then
			print("Saved "..Player.Name.."'s Data Successfully")
		else
			wait("Error: "..errormessage)
		end
	end
end

function module.FireInfomration(Player)
	game.ReplicatedStorage.sendOwned:FireClient(Player, module["Inventory-"..tostring(Player.UserId)].Trails)
	game.ReplicatedStorage.sendOwnedEffects:FireClient(Player,	module["Inventory-"..tostring(Player.UserId)].Effects)
end

game.ReplicatedStorage.sendOwned.OnServerEvent:Connect(function(Player, owned)
	module["Inventory-"..tostring(Player.UserId)].Trails = owned
end)

game.ReplicatedStorage.sendOwnedEffects.OnServerEvent:Connect(function(Player, owned)
	module["Inventory-"..tostring(Player.UserId)].Effects = owned
end)


game:BindToClose(module.SaveAllData)

return module

Chunk of code dedicated to setting instannce values:

	for indexValueName, Value in pairs(userData["PlayerData-"..tostring(player.UserId)]) do
		local TblOfValues = {CRW, benefits, rank, spleefCoins, wins, Levels, XP, XP_Limit, Equipped, Equipped}
		for i, v in pairs(TblOfValues) do
			if Value == v.Name then
				v.Value = Value
			end
		end
	end

Have you tried printing to see if the playerdatas values are the defaults via “Wins = 0”

1 Like

Will do, then I’ll update you on the issue

Its not nil, it printing all the default values. I think its an issue with my for i,v in pairs loop

try doing ipairs instead of pairs since you’re handling instances also I’ve just noticed you’re trying to compare a value to a instance name, you want to compare the indexValueName instead of Value otherwise you’re trying to compare the values you want to set instead of the names for matching

1 Like

I will do, I’m on mobile atm so maybe tomorrow