How do I save this into a datastore?

What are you attempting to achieve?

  • 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!

image

1 Like

Could you show us your code so far, along with any errors it throws?

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)]

Could you show us Line 75? Obviously it’s an error in that exact line.

1 Like

for nm, dw in pairs(e) do (line 75)

I’ve used this datastore code for my previous game so I’ve tried adding onto it and just hurt my brain.

Then “e” is a string for some reason. Only tables work for in pairs.

Yeah, I’m trying to save a table like this.

local table = {
 GunName = {
Color = true
Color = false
Color = false
}
}

Do you know a way I can do this?

Just to let you know, the e variable is just an integer, while the q variable is the actual variable of the contents of data.

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

Ok thanks.

Okay, so I’m getting the error

[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

I would avoid using instances to hold player data. Store player data in the script then just pass the table to save.

Edited the previous post, should work now.

1 Like

What are the advantages of holding it in a script compared to instances?

Can you test it out?

Doing so now.

Just tested, it appears to not error but I don’t believe it’s saving.
Either it’s not saving or it’s not loading the data.

As I thought, you are saving the wrong variable. You should be proceed the for loop with the q variable.

Will try that now.

Hm, saving the q variable isn’t doing anything either.