Add new key+value if needed

Hello,

I would like to know if you think it’s a good way. Firstly, I am using Firebase to have access anywhere.
Then, I created a script to create the data of the player from a format:

local FirebaseService = require(game:GetService("ServerScriptService"):WaitForChild('FirebaseService'))
local database = FirebaseService:GetFirebase("players");

game:GetService("Players").PlayerAdded:Connect(function(p)
	local dataFORMAT = database:GetAsync("FORMAT")
	
	local dataPlayer = database:GetAsync(p.UserId)
	
	if dataPlayer == nil then
		local sendData = game:GetService("HttpService"):JSONDecode(dataFORMAT)
		sendData["name"] = p.Name 
		database:SetAsync(p.UserId, game.HttpService:JSONEncode(sendData))
	end
	
	dataPlayer = database:GetAsync(p.UserId)
	
	local decode = game:GetService("HttpService"):JSONDecode(dataPlayer)
end)

By the way, you can see the informations here:
image

I get the data of the player and it has been already added to the database so there is no problem in the if. But, now I would like it add the updates if I add new key+value on my database from my FORMAT.

For example, I add a new key whose name is “Time” which give the time in-game, then if it’s does not exist in the data of the player, it add it.

I guess I can use a for loop to search if the value exist in the player and in the FORMAT but I do not know if there is a better way.

1 Like

Soo, you trying to do data store system, for player, if player is banned, what is rank ect… i can tell you, don’t use free kits, it maybe easy , but it’s not recommended, the best way is try to make your own system, i can give you my data storing system, this normal and advanced.

local ds = game:GetService("DataStoreService"):GetDataStore("plrData")
game.Players.PlayerAdded:Connect(function(plr)
wait(0)
	local key = "id_"..plr.UserId
	local stats = plr:FindFirstChild("stats")
	
print("stats located")
		
  local money = plr.stats.Money or nil
	
	local cap = plr.stats.Capacity or nil
	local bag = plr.stats.Backpack or nil
	local store = plr.stats.Storage or nil
	local tool = plr.stats.Tool or nil
	
	local data
	local succes, err = pcall(function()
		 data = ds:GetAsync(key)
	
	end)
	if succes then
		money.Value = data.money or nil
		cap.Value = data.cap or nil
		bag.Value = data.bag or nil
		store.Value = data.store or nil
		tool.Value = data.tool or nil
		print("Data Loaded")
		
	end

end)
game.Players.PlayerRemoving:Connect(function(plr)
	local key = "id_"..plr.UserId
	local data = {
		money = plr.stats.Money.Value;
		cap = plr.stats.Capacity.Value;
		bag = plr.stats.Backpack.Value;
		store = plr.stats.Storage.Value;
		tool = plr.stats.Tool.Value;
	
	}
	local succes, err = pcall(function()
		ds:SetAsync(key, data)
		
	end)
	if succes then
		print(plr.Name.." Data Has Been Saved")
	else
		print(plr.Name.." Do Not Have Saved Data...")
		warn(err)
		
	end
end)

and don’t use json code, if you wan’t save something like this, it’s only key space wasting, remember!, you have only 4 mb to store on one key, it’s a lot for roblox data, but do not think it’s infinite


Database is better datastore lol and no it’s not “easy” :rofl: !

I know what you wan’t to achieve, you wan’t to save multiple player data, that are unknown into data store, you wan’t to save a lot of propertiess, this is another more advanced script, how i achieved unknown value save, it’s simple for me, but if you have any question, ask

local DSS = game:GetService("DataStoreService")
local DS1 = DSS:GetDataStore("DS1")

game.Players.PlayerAdded:Connect(function(plr)
	local key = "id_"..plr.UserId
	local data 
	
	local succes,err = pcall(function()
		data = DS1:GetAsync(key)
	end)
	if succes and data then
		for i, v in pairs(data) do
			if v then
				if v["Type"] ~=nil then 
					if v["Type"] == "ToSave" then
						
						
						local new = Instance.new("StringValue",plr.Stats.Abilities)
						new.Name = v.Name
						
						local Type = new:SetAttribute("Type", v["Type"])
						local Damage = new:SetAttribute("Damage",v["Damage"])
						local Equip = new:SetAttribute("Equip",v["Equip"])
						
					end
				end
			end
			if i == #data then
				break
			end
		end
		
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local key = "id_"..plr.UserId
	local PlayerData = {}
	for i, v in pairs(plr.Stats.Abilities:GetChildren()) do
		if v then
			if v:GetAttribute("Type") and v:GetAttribute("Type") == "ToSave" then
				local Data = {
					Name = v.Name,
					Type = v:GetAttribute("Type"),
					Damage = v:GetAttribute("Damage"),
					Equip = v:GetAttribute("Equip")
					
				}
				table.insert(PlayerData,Data)
			end
		end
		if i == #plr.Stats.Abilities:GetChildren() then
			break
		end
	end
	

	local succes,err = pcall(function()
		DS1:SetAsync(key,PlayerData)
	end)
	if succes then 
		print("Saved")
	else
		warn(err)
	end
end)

Is there a reason your not just using DatastoreService? Is it because your trying to update data from discord or trello?

For example yes and database are better than DS on Roblox.

I don’t see a reason to use third party datastore services. If they go down, so does your game. If you’re not using it to save data between games, or through other programs outside of roblox, you should just use Datastores on roblox.

We can download the data so do not worry about it.

Btw, you have not idea to do a “checker keyvalue” on Roblox?

Okay, so to add a key if it doesn’t exist, you could do this.

local data -- user's data here
data.Time = data.Time or 0
-- every second (Don't save every second):
data.Time += 1
1 Like

I want to check only if it does not work exist.

if not data.Time then print("Time wasn't assigned") end
1 Like

Yeah but there is not a way to check if any value in data does not exist? Like if you want to compare 2 players and if someone has not the key of the other, it add it.