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

Data not saving using tables?

I want to try a new way on making datastores, so I made tables for the values. The problem is, it is not saving my data.

local dataStore = game:GetService('DataStoreService'):GetDataStore('Data0')

local valueName = {
	'EXP',
  	'Level',
  	'Points'
}
local valuesValue = {
	'IntValue',
  	'IntValue',
  	'IntValue'
}

game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new('Folder' ,plr)
    stats.Name = 'Stats'
    local key = "Player-"..plr.UserId
    for i = 1, 3 do
    	value = Instance.new(valuesValue[i], stats)
      	value.Name = valueName[i]
    	value.Value = dataStore:GetAsync(key,valueName[i]) or valuesValue[i]
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = 'Player-'..plr.UserId
    for i = 1, 3 do
    	value.Value = dataStore:SetAsync(key,valueName[i])
    end
end)

2 comments

1

Please explain whats wrong. you didn't explain anything about your script.

1

You'll be overwriting the same key three times when you save. You should probably just save the table itself.

1 answer

From what I see, this part of code..

game.Players.PlayerRemoving:connect(function(plr)
    local key = 'Player-'..plr.UserId
    for i = 1, 3 do
        value.Value = dataStore:SetAsync(key,valueName[i])
    end
end)

Most of your code is correct, but so far, this should be the only problem I've spot.

From your code, above

  1. You're overwriting the key 3 times.
  2. You're saving a "part" of the table, or what I can say, a ordinary string.
  3. You're setting a value into a value (WHAAT!?)

For sure, if you want this to be easily done. Perhaps this is my suggestion WARNING: Code not checked

local dataStore = game:GetService('DataStoreService'):GetDataStore('Data0')
local HS = game:GetService("HttpService") -- JSON Derp.


local valueName = {
    'EXP',
    'Level',
    'Points'
}

local valuesValue = {
    'IntValue',
    'IntValue',
    'IntValue'
}

local defaultValue = { -- No one likes default values. but this should've helpt
	0,
	0,
	0
}

--Will depend on valueNames

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new('Folder' ,plr)
    stats.Name = 'Stats'
    local key = "Player-"..plr.UserId
	local data = dataStore:GetAsync(key)
	
	print(data)	
	
	if type(data) == "string" then
		data = HS:JSONDecode(data)
	else
		data = {}
	end
	
	for k,v in pairs(valueName) do -- Perhaps adding a DataStore specific table key would be good.
		if not data[v] then
			data[v] = defaultValue[k]
		end
	end

    for k,v in pairs(valueName) do -- Should help, do you really need to use numbers?
        local value = Instance.new(valuesValue[k], stats)
        value.Name = v
        value.Value = data[v] -- Like I said, DataStore specific table keys, might help. (Or add more conflict.)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = 'Player-'..plr.UserId
   	local PlayerData = plr:findFirstChild("Stats")
	if not PlayerData then print("Uhh... Saving Failed?") return end
	local data = {}
	-- I think saving based on valuenames would be better, in case of extra fields.
	for k,v in pairs(valueName) do -- Should help, do you really need to use numbers?
        local dat = PlayerData:findFirstChild(v)
		if not dat then return end
		data[v] = dat.Value
	end
	print(HS:JSONEncode(data))
	dataStore:SetAsync(key, HS:JSONEncode(data))
end)


Theoritically, should work.

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