Problem with DataStore script

Hello everyone, I am having an issue with my data store script since my last post. I wanted to add a code system.

I tried checking the code and playtesting multiple times but non of these methods worked.

Server Script:

local Services = {
	Players = game:GetService("Players");
	DataStoreService = game:GetService("DataStoreService");
	RunService = game:GetService("RunService");
	HttpService = game:GetService("HttpService");
}

local DataStores = {}

local resources = {"Gold", "Shells", "Coconuts", "Fish", "Pebbles", "Leaves", "Banned"}
local codes = {}
local codesDatabase = require(	script["Codes System"].Database)

delay(0, function()
	for i, v in pairs(codesDatabase.Codes) do
		table.insert(codes, i)
	end
	
	for _, resource in pairs(resources) do
		DataStores[resource .. "Data"] = Services.DataStoreService:GetDataStore(resource .. "Data")
	end

	for _, code in pairs(codes) do
		DataStores[code .. "Data"] = Services.DataStoreService:GetDataStore("Code"..code .. "Data")
	end

end)


local function SavePlayerData(player)
	local success, errormessage = pcall(function()
		local playerdata = {}
		for i, v in pairs(player.Data:GetChildren()) do
			playerdata[v.Name] = v.Value
		end
		
		for i, v in pairs(player.Codes:GetChildren()) do
			playerdata[v.Name] = v.Value
		end
		
		for i, v in pairs(DataStores) do
			v:SetAsync(player.UserId, playerdata[i])
		end
	end)
	if not success then
		warn(errormessage)
	end
end

local function LoadPlayerData(player)
	local success, errormessage = pcall(function()
		if player:FindFirstChild("Data") then
			local playerdata = {}
			for i, v in pairs(DataStores) do
				playerdata[i] = v:GetAsync(player.UserId) or 0
			end
			for i, v in pairs(playerdata) do
				player.Data:FindFirstChild(i).Value = v
			end
			if playerdata["Banned"] then
				player:Kick("You have been kicked from the game due to being banned for prohibited client-server behavior. | If you believe that this is a mistake, contact our staff.")
			end
		end
		
		if player:FindFirstChild("Codes") then
			local playerdata = {}
			for i, v in pairs(DataStores) do
				playerdata[i] = v:GetAsync(player.UserId) or 0
			end
			for i, v in pairs(playerdata) do
				player.Codes:FindFirstChild(i).Value = v
			end
		end
	end)
	if not success then
		warn(errormessage)
	end
end

Services.Players.PlayerAdded:Connect(function(player)
	local DataFolder = Instance.new("Folder", player)
	DataFolder.Name = "Data"
	
	local CodesFolder = Instance.new("Folder", player)
	CodesFolder.Name = "Codes"
	
	local BannedValue = Instance.new("BoolValue", DataFolder)
	BannedValue.Name = "Banned"
	
	delay(0, function()
			for _, resource in pairs(resources) do
		local value = Instance.new("NumberValue", DataFolder)
		value.Name = resource.."Data"

		if Services.RunService:IsStudio() then
			value.Changed:Connect(function()
				SavePlayerData(player)
			end)
		end
	end
	
	for _, code in pairs(codes) do
		local value = Instance.new("BoolValue", CodesFolder)
		value.Name = "Code"..code.."Data"

		if Services.RunService:IsStudio() then
			value.Changed:Connect(function()
				SavePlayerData(player)
			end)
		end
	end
	end)

	player.CharacterAdded:Wait()
	LoadPlayerData(player)
end)

Services.Players.PlayerRemoving:Connect(function(player)
	SavePlayerData(player)
end)

game:BindToClose(function()
	for i, player in pairs(Services.Players:GetPlayers()) do
		SavePlayerData(player)
	end
end)

Error | Output:

ServerScriptService.Data.Server:133: attempt to index nil with ‘Value’ - Server - Server:151
Argument 2 missing or nil - Server

And here is line 133:

player.Data:FindFirstChild(i).Value = v

Any help is greatly appreciated!

Could you print(playerdata) and see what its returning, just right there before the loop on line 132

player.Data:FindFirstChild(i) is giving you a nil value. When you are trying to index into it with .Value, it throws that error. Make sure you are checking if that value actually exists before you index into it, or if it should exist, figure out what kind of logic error you made.

▼ {
[“BannedData”] = 0,
[“CoconutsData”] = 0,
[“FishData”] = 0,
[“GoldData”] = 1000,
[“LeavesData”] = 0,
[“PebblesData”] = 0,
[“ShellsData”] = 0,
[“TEST1Data”] = 0
} - Server - Server:132

the code isn’t supposed to be a number value though.

This is generally a bad practice, it does not serve any purpose and doing so is just getting a nil value.
if you have no intention in checking if the object exist or not (although you should), you should instead just write down
player.Data[i].Value = v

For this problem, it usually mean you have changed the data’s originally saved name, so when they’re called to load in, it attempt to load its original variables and turns out those one went missing and give you an error.

the solution is literally just changing the line from
player.Data:FindFirstChild(i).Value = v
to
if player.Data:FindFirstChild(i) then
player.Data[i].Value = v
end

I believe this would still cause an error though, since player.Data[i] is nil regardless of which function you use to get it

can you elaborate on which one you’re talking about

But it still worked in my previous post.
Edit: Same error.

I missed the code you wrote at the end, sorry. That would work but I think in the context of this script, all the values being parsed over should return a value. Then again it’s not my script and I don’t know exactly what it should be doing

Make sure everything you expect is in player.Data. I can imagine if you printed out player.Data the solution might become clearer

uhhh… previous post? Have you added/removed anything while your data was working as intended.

I added a code system. Basically the data saving of the codes.

yes, as I previously stated, this come from the result of changing how the data store things resulting in unused data or data that no longer exist.
I recommend just adding findfirstchild to check if that data still exist as a fail guard.

It fixed the error, ty! But as I stated before in the post, there is a second error while saving the data.
Argument 2 missing or nil - Server - Server:46

It comes from this function:

local function SavePlayerData(player)
	local success, errormessage = pcall(function()
		local playerdata = {}
		for i, v in pairs(player.Data:GetChildren()) do
			playerdata[v.Name] = v.Value
		end
		
		for i, v in pairs(player.Codes:GetChildren()) do
			playerdata[v.Name] = v.Value
		end
		
		for i, v in pairs(DataStores) do
			v:SetAsync(player.UserId, playerdata[i])
		end
	end)
	if not success then
		warn(errormessage)
	end
end

Line 46: warn(errormessage)

uhhhh… can you tell me what did it warn you…?

Just what I said: Argument 2 missing or nil

Are you sure it’s on the warn call? Warn can accept a number of arguments but 1 should also be fine

It’s on the warn call indeed. I clicked on it.

try printing the error message there before warn

Argument 2 missing or nil - Server - Server:45
Argument 2 missing or nil - Server - Server:47

image