Too much values to save in datastore


I’m making a weapon customizer, this really need to store a lot of values, as you can see i have a lot of values to instance, but is there any way to make it easier/another way to create and save values ?

4 Likes

I’m not that experienced with datastore but try making a table that will save everything needed

You could utilize for loops:

for i = 1, 7, 1 do

local Union = instance.new("Color3Value", player)
Union.Name = "Union" .. i

end
1 Like

It’s also a good variant, but also i need to save all of this values, that’s going to be a big problem

1 Like

In my opinion it wouldn’t be difficult, do you already have datastore code?

i’ve got an idea on how to save every value, i’m going to try, i think i can just do:

local success, err = pcall(function()
for _,v in pairs(player:GetChildren()) do
if v:IsA(“NumberValue”) or v:IsA(“Color3Value”) or v:IsA(“StringValue”) then
dataStore:SetAsync(player.UserId, v)
end
end
end)

1 Like

And what about loading values?
Smoothing.Value = dataStore:GetAsync(player.UserId)
do i need to getasync for every value?

save it as a table. Use JsonEncode to reduce the size further

how do i save it as table?

30 letters

I’m not sure how your customization is set up, so I’ll provide an example of getting all the baseparts in a model and save their properties to a table

local model

local saveTable = {}
local id = httpService:GenerateGUID(false) --set a unique identifier for the model, or else it will override the existing settings of another weapon 
saveTable[id] = {}

for _, part in pairs (model:GetChildren()) do
	if part:IsA("BasePart") then
		local texture = part:FindFirstChildOfClass("Texture")
		if texture then
			texture = texture.Texture
		end
		saveTable[id][#saveTable[id]+1] = {
			Transparency = part.Transparency,
			Texture = texture,
			Material = part.Material.Name
		}
	end
end

you can then update the data just once, instead of calling SetAsync for every value, something like

datastore:UpdateAsync("key", function(oldData)
	local newData = {}
	if oldData then
		newData = httpService:JSONDecode(oldData)
	end
	newData["Customizations"] = saveTable
	
	local encodedData = httpService:JSONEncode(newData)
	return encodedData
end)
3 Likes

I have a question, this code you sent is saving and updating data and how do i load it ?

I haven’t looked at all of your code, and I’m not a PRO at scripting but here’s a loop I used to save my data:

game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder", player)
folder.Name = "Stats"

local guide = {[1] = "Health",[2] = "Stamina", [3] = "Meelee", [4] = "Sword", [5] = "DevilFruit", [6] = "Gun", [7] = "ArmamentHaki", [8] = "ObservationHaki", [9] = "ConquerorsHaki"}

for i = 1, 9 do
	
	local x = Instance.new("IntValue", folder)
	x.Name = guide[i].."Level"
	x.Value = 1
	
	local x2 = Instance.new("IntValue", folder)
	x2.Name = guide[i].."Exp"
	x2.Value = 0
	
	local x3 = Instance.new("IntValue", folder)
	x3.Name = guide[i].."MaxExp"
	x3.Value = 100
	
	x2.Changed:Connect(function()
		if x2.Value >= x3.Value then
			x2.Value = 0
			x.Value = x.Value + 1
		end
	end)
	
end

local success, errorm = pcall(function()
	
	local loadedData = DataStore:GetAsync(player.UserId)
	
	for i, v in pairs(loadedData)  do
		if folder:FindFirstChild(i) then
			folder:FindFirstChild(i).Value = v
		end
	end
	
end)

if not success then
	print(errorm)
end

end)

2 Likes

i mean to use it for my weapons, for example: then player equip tool it will change it color and material

sorry for the late reply

you can load the data whenever the player joins and store their data in a table, then when the player wants to equip the weapon, you can run the customization, below is an example

local allPlayerDatas = {}

local function EquipWeapon(player, id)
	local customizationData = allPlayerDatas[player.Name]["Customizations"]
	local weaponModel = player.Character:FindFirstChild(id) --assuming the player has the weapon and the weapon is named the same as its unique identifier
	if weaponModel and customizationData[id] then --if weapon exist and if it has customization set
		for i=1, #customizationData[id] do
			if customizationData[id][i]["PartName"] then
				local partInWeapon = weaponModel:FindFirstChild(customizationData[id][i]["PartName"])
				if partInWeapon then
					partInWeapon.Transparency = customizationData[id][i]["Transparency"]
					partInWeapon.Material = Enum.Material[customizationData[id][i]["Material"]]
				end
			end
		end
	end
end

if you have a folder that store and clone weapons whenever the player equip them, clone the weapon first, run the customization, then parent it to the character

2 Likes

Thank you for explaining me a lot of things about datastores! Have a good day / night!

1 Like

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