Anyone know why my datastore script only saves 1 numbervalue?

Whenever i insert 2 NumbersValues into Data_Ids and leave and rejoin for loading, only 1 of the NumberValues is loaded. Sorry for creating such messy code.

Datastore script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Data_Storee_SamthekidRSsl")

game.Players.PlayerAdded:Connect(function(Player)
	local Data_Ids = Instance.new("Folder")
	Data_Ids.Parent = Player
	Data_Ids.Name = "Data_Ids"
		
	local data
	local loadedData, errormessage = pcall(function()
		data = DataStore:GetAsync(Player.UserId) 
	end)
	
	if loadedData and data ~= nil then
		print(_G.NameValue, _G.ValueValue)
		
		local String_Id_Loaded = Instance.new("NumberValue")
		String_Id_Loaded.Parent = Player.Data_Ids
		String_Id_Loaded.Name = data[1]
		String_Id_Loaded.Value = data[2]
	else
		warn(Player.Name.. " Has no data make them data or die")
	end
	
	
	Player.Chatted:Connect(function(msg)
		local function Get_Name_To_Save(msg)
		return msg:match("/([%w%s]-)$");
	end
	
	if msg:sub(1,5) == "save/" then
		_G.Id_To_Save = msg.match(msg, "[%d%s]+")
		_G.Name_To_Save = Get_Name_To_Save(msg)
		
		local success, err = pcall(function()
			local String_Id = Instance.new("NumberValue")
			String_Id.Parent = Data_Ids
			String_Id.Name = _G.Name_To_Save
			String_Id.Value = _G.Id_To_Save
		 end)
	      end
        end)	
   end)

game.Players.PlayerRemoving:Connect(function(Player)
	_G.id = Player.UserId
	local Save_Id = Player.Data_Ids
	
	for ind, decend in pairs(Save_Id:GetDescendants()) do
		print(decend)
		_G.NameValue = decend.Name
		_G.ValueValue = decend.Value
	end	
end)

game:BindToClose(function()
	DataStore:SetAsync(_G.id, {_G.NameValue, _G.ValueValue})
	wait(2)
end)
1 Like

I do get this error every now and then whenever i execute it, because it doesn’t set String_Id_Loaded.Name properly
dddd

There are many issues with your script here. Including the fact you are setting data using _G.NameValue and/or _G.ValueValue.

The reason why this is bad is because it is not associated with a specific player. So if two or more players leave at the exact same time, who’s data will they receive?

I have a tutorial I made a while ago which I think you should look in to:

2 Likes