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

Why isnt this DataStoring script saving my 2nd value?

What is wrong with this DataStorage script?

It saves "SaveValue", but it does not save "Regular", what am I doing wrong?

Here is my coding:

game.Players.PlayerAdded:connect(function(plr)
	local uniquekey = "id-"..plr.userId
	local leaderstats = Instance.new("IntValue")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	
	local savevalue = Instance.new("IntValue")
	savevalue.Parent = leaderstats
	savevalue.Name = "Coins"
	
	local Regular = Instance.new("IntValue")
	Regular.Parent = plr
	Regular.Name = "Regular"
	
	--GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		Regular.Value = GetSaved[2]
	else
		savevalue.Value = 1000000
		Regular.Value = 0
		local NumbersForSaving = {savevalue.Value, Regular.Value}
		DSService:SetAsync(uniquekey,NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = "id-"..plr.userId
	local SaveCoins = {plr.leaderstats.Coins.Value}
	local SaveRegular = {plr.Regular.Value}
	DSService:SetAsync(uniquekey,SaveCoins)
	DSService:SetAsync(uniquekey,SaveRegular)
end)

Any help is greatly appreciated!

1 answer

✓ Accepted answer

The problem is the way you are saving the data, one key can only hold one value. Setting the key again overwrites the previous value stored.

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = "id-"..plr.userId
    local SaveCoins = {plr.leaderstats.Coins.Value}
    local SaveRegular = {plr.Regular.Value}
    -- data saved 
    DSService:SetAsync(uniquekey,SaveCoins)
    -- data overwritten 
    DSService:SetAsync(uniquekey,SaveRegular)
end)

Other notes:-

You should be checking the data that is being loaded as there is still a possibility of it being nil, we can simply use plrData[1] or 0 which will return 0 if the first argument is nil.

It is best to create a function for saving and loading as this is usually done on multiple occasions so we can just reuse the function.

There are a lot of possible errors which can also occur which are not managed in the example below.

Example:-

local dsServ = game:GetService('DataStoreService')
local dataStore = dsServ:GetDataStore(' name of data store')

local function getKey(plr)
	return "id-"..plr.userId
end

local function loadData(plr)
	return dataStore:GetAsync(getKey(plr))
end

local function saveData(plr, data)
	dataStore:SetAsync(getKey(plr), data)
end

game.Players.PlayerAdded:connect(function(plr)
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	
	local savevalue = Instance.new("IntValue", leaderstats)
	savevalue.Name = "Coins"
	
	local Regular = Instance.new("IntValue")
	Regular.Name = "Regular"
	
	-- it is more efficent to set the parents last
	-- http://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296
	leaderstats.Parent = plr
	Regular.Parent = plr
	
	local plrData = loadData(plr)
	if plrData then
		savevalue.Value = plrData[1] or 0
        Regular.Value = plrData[2] or 0
	else
		savevalue.Value = 1000000
        Regular.Value = 0
		saveData(plr, {savevalue.Value, Regular.Value})
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	saveData(plr, {plr.leaderstats.Coins.Value, plr.Regular.Value})
end)

I hope this helps.

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