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

How to save another value in Data Storage?

Inside of my data storage script, I want to add more values. For example, there is a value called "Cash" inside of a value called Levelss. I'd like to add another value, the same exact way that Cash is added, but every time I do, something went wrong. I'm still very new to Data Storage. Any ideas?

local DSService = game:GetService('DataStoreService'):GetDataStore('DataStoreName')
game.Players.PlayerAdded:connect(function(plr)
	
	local uniquekey = 'id-'..plr.userId
	local Levelss = Instance.new('IntValue', plr)
	local savevalue =  Instance.new('IntValue')
	Levelss.Name = 'Levelss'
	savevalue.Parent = Levelss
	savevalue.Name = 'Cash'
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
		local NumbersForSaving = {savevalue.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.Levelss.Cash.Value}
DSService:SetAsync(uniquekey, Savetable)	
	
	
end)

1 comment

0

What value are you trying to add and what's the error?

1 answer

✓ Accepted answer

Didn't test it but it should work.

local DSService = game:GetService('DataStoreService'):GetDataStore('Stats')
ID = 0 --If you change this then it will reset everyones stats
SaveTime = 60 --Shouldn't be less then 60
leaderboardname = "Levelss" --The name of the leader board

StatList = {Cash = 0, StartingMoney = 5}
--StartingMoney would give new players the stat StartingMoney with the value 5
--Put any of your values here
--Values are loaded when the player joins and saved when they exit
--Values are also saved every SaveTime seconds

----- You shouldn't need to change anything below this line -----

function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
	Value = Value or 0
	local savevalue =  Instance.new('IntValue', Parent)
	savevalue.Name = Name
	savevalue.Value = Value
	return savevalue
end

function GetValue(uniquekey)
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		return GetSaved
	end
end

game.Players.PlayerAdded:connect(function(plr)
	local uniquekey = ID..plr.userId
	local Levelss = Instance.new('IntValue', plr)
	Levelss.Name = leaderboardname
	for i,v in pairs(StatList) do
		local Stat = GetValue(i..uniquekey)
		if Stat then
			CreateValue(i, Levelss, Stat) --Old player load their value
		else
			CreateValue(i, Levelss, v) --New player load the default value
		end
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = ID..plr.userId
	if plr:FindFirstChild(leaderboardname) then
		for i,v in pairs(StatList) do
			if plr[leaderboardname]:FindFirstChild(i) then
				DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
			end
		end
	end
end)

while wait(SaveTime) do
	P = game:GetService("Players"):GetChildren()
	for o=1, #P do
		if P[o] then
			local uniquekey = ID..P[o].userId
			if P[o]:FindFirstChild(leaderboardname) then
				for i,v in pairs(StatList) do
					if P[o][leaderboardname]:FindFirstChild(i) then
						DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
					end
				end
			end
		end
	end
end

4 comments

0

Your script works but the stats will not load in with the value they are set to, startingmoney is still 0

0

Missed a line, fixed it

0

It worked, thanks.

0

Your welcome

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