Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datastore not loading data correctly?

DataBaseStore = game:GetService('DataStoreService'):GetDataStore('Sim:002')
local datafolder = script.Data

game.Players.PlayerAdded:Connect(function(Player)
	
	local key = "PlayerID=" .. Player.userId
	local GetSave = DataBaseStore:GetAsync(key)
	
	local datas = datafolder:Clone()
	datas.Parent = Player
	
	if GetSave then
		-- player has data
		
		for i, dataf in pairs(datafolder:GetChildren()) do
			if dataf:IsA('Folder') then
				for i,v in pairs(dataf:GetChildren()) do
					local name = tonumber(v)
					v.Value = GetSave[name]
					print('Data loaded for ' .. i, v, v.Value)
				end
			end
		end
		
	else
		
		local statstosave = {}
		
		for i, dataf in pairs(datas:GetChildren()) do
			if dataf:IsA('Folder') then
				for i, folderitems in pairs(dataf:GetChildren()) do
					table.insert(statstosave, folderitems.Value)
					print(i, folderitems.Name, folderitems.Value)
				end
			end
		end
		DataBaseStore:SetAsync(key, statstosave)
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	
	local key = "PlayerID=" .. Player.userId
	
	local datas = Player:WaitForChild('Data')
	
	local statstosave = {}
		
	for i, dataf in pairs(datas:GetChildren()) do
		if dataf:IsA('Folder') then
			for i, folderitems in pairs(dataf:GetChildren()) do
				table.insert(statstosave, folderitems.Value)
				print(i, folderitems.Name, folderitems.Value)
			end
		end
	end
	DataBaseStore:SetAsync(key, statstosave)
end)

I get this error on line 21 - 00:49:16.475 - ServerScriptService.Data:21: bad argument #3 to 'Value' (string expected, got nil)

Edit - heres the folder and its values im trying to save: http://prntscr.com/o16951

2 comments

0

have an event for game:BindToClose() so data will save once the game shut downs or something

0

i will add the once i fix this problem

1 answer

instead of just having local GetSave = DataBaseStore:GetAsync(key) you should have something thats more secure. more like

local GetSave
pcall(function()
	GetSave = DataBaseStore:GetAsync(key)
end)

and also v.Value will be nil due to you doing GetSave[name] on line 19 instead of GetSave["name"]

4 comments

0

sadly that didnt work

0

thats wrong on many levels

0

UnitRook, what is tonumber(v) suposed to output?

0

Its supposed to turn the values name into a number

This discussion is preserved as it was. New answers, comments, and votes are closed.