How can I save values inside of folders?

Hey guys. I am trying to make an attachment system for guns but I want them to save. But for that I need to somehow save the values of the attachments as shown here:
image

I have tried using datastore but it does not work, and I know I am using something wrong, so how can I do this? Thank you for your help.

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

local guns = Instance.new("Folder",p)

guns.Name = "WeaponAttachments"

wait(4)

for i,v in pairs(p:WaitForChild("Backpack"):GetChildren()) do

-- print(v.Nlame)

local weapon = Instance.new("Folder",guns)

weapon.Name = v.Name

local at1 = Instance.new("StringValue", weapon)

at1.Name = "Optic"

local at2 = Instance.new("StringValue", weapon)

at2.Name = "Barrel"

local at3 = Instance.new("StringValue", weapon)

at3.Name = "UnderBarrel"

local at4 = Instance.new("StringValue", weapon)

at4.Name = "Other"

end

end)
1 Like

Do you have the Studio Access to API Services enabled? And what did you save with datastore (the folder or the values)?

Did you use regular datastore service or did you use datastore2 (which is better and I have no idea why)?

1 Like

Here’s how you could structure a table for your data store:

--top of the script
local WeaponAttachments = --the directory to your folder

--somewhere in your saving system

local weaponAttachments = {}

for i, v in pairs(WeaponAttachements:GetChildren()) do
 weaponAttachments[v.Name] = {
    ["Barrel"] = v.Barrel.Value
    ["Optic"] = v.Optic.Value
    --and so on
  }
end
3 Likes

We cant help you without showing your code man

1 Like

sorry i forgot to add the code. I added it but I don’t think it will help much

Can you show the code for the datastore?

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("Attachments")

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

	local items = plr:WaitForChild("WeaponAttachments")
	

	local save1 = ds:GetAsync(plr.UserId)



	if save1 then
		for i, tool in pairs(save1) do
			--Here is what I do not know what to do.
		end
		end
	end)

game.Players.PlayerRemoving:Connect(function(player)
	local attachments = {}
	for i, name in pairs(player.WeaponAttachments:GetChildren()) do
		table.insert(attachments,name.Name)
	end
	ds:SetAsync(player.UserId, attachments)

end)

I also think this is wrong and you are not supposed to do this. I am very new to coding so sorry.

2 Likes

so like this?

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

local guns = Instance.new("Folder",p)
guns.Name = "WeaponAttachments"
wait(4)
for i,v in pairs(p:WaitForChild("Backpack"):GetChildren()) do
	--	print(v.Nlame)
	local weapon = Instance.new("Folder",guns)
	weapon.Name = v.Name
	local at1 = Instance.new("StringValue", weapon)
	at1.Name = "Optic"
	local at2 = Instance.new("StringValue", weapon)
	at2.Name = "Barrel"
	local at3 = Instance.new("StringValue", weapon)
	at3.Name = "UnderBarrel"
	local at4 = Instance.new("StringValue", weapon)
	at4.Name = "Other"
	end	
	
	local WeaponAttachments = p:WaitForChild("WeaponAttachments") --the directory to your folder

		--somewhere in your saving system

	local weaponAttachments = {}

	for i, v in pairs(WeaponAttachments:GetChildren()) do
		weaponAttachments[v.Name] = {
			["Barrel"] = v.Barrel.Value
			["Optic"] = v.Optic.Value
			--and so on
		}
	end
end)
1 Like

Is this code supposed to save or load the weapon attachments? I’m not used to Roblox’s datastore, I’m used to DS2.

Me and Dev_Peashie have covered this in the past on how to save folders, check it out it might help:

1 Like

Sorry to bump, but you don’t need to use :WaitForChild for things inside the player. Only use WaitForChild for objects inside the character right when they join.

We could say that if it was apart of the player constructure, but it isnt, its a folder that is stored inside of the player upon joining, so you still neef to use WaitForChild to be safe

Also if this fixed your issue please mark it as a solution so others can get their thing fixed

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