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:
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)
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
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.
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)
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