I require some help with my datastore

Hello! I am not very sure which bit of the script is erroring because there is no error output, but this is the script for Loading the data, saving the data and the table of the data

Data Table

local NPC_Data = {
	['Money'] = 0,
	['NPCs'] = {
		[1] = {
			['Rank'] = 'Private',
			['Lvl'] = 1,
			['XP'] = 0,
			['XPN'] = 100
		},
		[2] = {
			['Rank'] = 'Sergeant',
			['Lvl'] = 1,
			['XP'] = 0,
			['XPN'] = 100
		},
	}
}

Loading Data

local leaderstats = Instance.new('Folder',PLR)
	leaderstats.Name = 'leaderstats'
	
	local money = Instance.new('IntValue',leaderstats)
	money.Name = 'Money'
	

	local NPC = Instance.new('Folder',PLR)
	NPC.Name = 'NPC'
	
	for i, v in pairs(NPC_Data['NPCs']) do
		local NPC_1 = Instance.new('Folder',NPC)
		NPC_1.Name =  tostring(i)
		
		local Rank = Instance.new('StringValue',NPC_1)
		Rank.Name = 'Rank'
		Rank.Value = v['Rank']
		
		local Lvl = Instance.new('IntValue',NPC_1)
		Lvl.Name = 'Lvl'
		Lvl.Value = v['Lvl']
		
		local EXP = Instance.new('IntValue',NPC_1)
		EXP.Name ='EXP'
		EXP.Value= v['XP']
		
		local XPN = Instance.new('IntValue',NPC_1)
		XPN.Name ='XPN'
		XPN.Value= v['XPN']
		

			local success, data = pcall(function()
				return DS:GetAsync(PLR.UserId)
			end)
			
			if success then
				for i, v in pairs(data['NPCs']) do
					NPC[tostring(i)]['Rank'].Value = v['Rank']
					NPC[tostring(i)]['Lvl'].Value = v['Lvl']
					NPC[tostring(i)]['EXP'].Value = v['XP']
					NPC[tostring(i)]['XPN'].Value = v['XPN']
					
				end
				
				money.Value = data['Money']
				
			else
				AddNpc(NPC,'Private')
				
			end
			
		end

Saving the data

game.Players.PlayerRemoving:Connect(function(PLR)
	local money = PLR.leaderstats.Money
	local NPC = {}
	
	for i, v in pairs(PLR.NPC:GetChildren()) do
		if v:IsA('Folder') then
			NPC[tostring(i)] = {
				Rank = v.Rank.Value,
				Lvl = v.Lvl.Value,
				XP = v.EXP.Value,
				XPN = v.XPN.Value
				
			}
			
		end
	end
	
	DS:SetAsync(PLR.UserId, {Money=money.Value, NPCs=NPC})
end)
game.Players.PlayerRemoving:Connect(function(PLR)
	wait(5)
	workspace.Npcs[PLR.Character.Name]:Destroy()
end)

i will always be a number here, so either remove the tostring( or change it to v.Name instead.

I have the NPC names as “1”,”2”,”3”, and keeps going to 30, each npc added is another number going up

Well the children order is unreliable and you should either sort it or loop through it differently.

So it doesn’t matter which name I put for the npc, it will still loop though all of them?

O wait I see what you mean by changing it to v.Name

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