I’m really confused on how to save players owned gun skins for each gun.
What is the issue?
I can’t seem to save it properly
What solutions have you tried so far?
I’ve tried using open source datastores but the code I’ve put together is too confusing for me to fix and is giving me a headache. (and I can only get it to work for Gun > Owned and not Gun > Skin > Owned)
I’m open to all suggestions on the best way to save this data. (looking to rescript my datastore script as it’s literally crap and confusing)
Thanks!
function generateDataTable(player, folder, datastore, ifSkin) -- With the skins, we have skin colors for each guns so we need to loop twice. ifSkin will be true or false
local iTable = {}
local newTable = {}
for index,v in pairs(folder:GetChildren()) do
if ifSkin == true then
for index2, z in pairs(v:GetChildren()) do
print(v.Name, z.Name, z.Owned.Value)
newTable[z.Name] = z.Owned.Value
iTable[v.Name] = newTable[z.Name]
end
else
iTable[v.Name] = v:WaitForChild('Owned').Value
end
end
for i,v in pairs(iTable) do
print(i)
print(v)
end
return(iTable)
end
function saveDataForPlayer(player, folder, datastore, ifSkin)
local key = "uid_"..player.UserId
local data = generateDataTable(player, folder, datastore, ifSkin)
datastore:SetAsync(key, data)
end
function loadData(player, myFolder, datastore, folderToUse, ifSkin)
local key = "uid_"..player.UserId
local data = datastore:GetAsync(key)
inputDataToPlayer(player, data, myFolder, folderToUse, ifSkin)
end
function inputDataToPlayer(player, data, myFolder, folderToUse, ifSkin)
local f1 = folderToUse
if data ~= nil then
print('Uploading data')
for i,v in pairs(f1:GetChildren()) do
for e, q in pairs(data) do
if v.Name == e then
if ifSkin == true then
for nm, dw in pairs(e) do
warn(dw.Name)
warn(nm.Name)
end
warn(v.Name, q) --q.Owned.Value)
-- v:WaitForChild('Owned').Value = q
else
v:WaitForChild('Owned').Value = q
end
end
end
end
else
error('Data is nil')
end
end
This is the code I’ve got at the moment but I’ve confused myself WAY too much.
The error is that it’s only saving Gun and Gold and not Gun, Gold and the owned value.
[15:19:53.450 - ServerScriptService.Shop System:75: bad argument #1 to ‘pairs’ (table expected, got string)]
Yeah I was attempting to save a table in a table so I thought it would reference my second table? I just need to know how to correctly do this so I can rewrite the entire script.
local TableOfValues = {}
for e, q in pairs(data) do
if v.Name == e then
if ifSkin == true then
TableOfValues[tonumber(#TableOfValues+1)] = e
for nm, dw in pairs(TableOfValues) do
warn(dw.Name)
warn(nm.Name)
end
warn(v.Name, q) --q.Owned.Value)
-- v:WaitForChild('Owned').Value = q
else
v:WaitForChild('Owned').Value = q
end
end
end
[15:54:45.729 - ServerScriptService.Shop System:78: bad argument #1 to ‘insert’ (table expected, got string)]
I think this is to do with generating my data table.
function generateDataTable(player, folder, datastore, ifSkin) -- With the skins, we have skin colors for each guns so we need to loop twice. ifSkin will be true or false
local iTable = {}
local newTable = {}
for index,v in pairs(folder:GetChildren()) do
if ifSkin == true then
for index2, z in pairs(v:GetChildren()) do
print(v.Name, z.Name, z.Owned.Value)
newTable[z.Name] = z.Owned.Value
iTable[v.Name] = newTable[z.Name]
end
else
iTable[v.Name] = v:WaitForChild('Owned').Value
end
end
for i,v in pairs(iTable) do
print(i)
print(v)
end
return(iTable)
end