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

Why will my datastore only save one value?

In my game there is a coins and level value and the coins you get in the game are on a base number multiplied by your level value. I have a datastore for level and coin values, but only the coins value is saving. Here is the main server side script in sever script service that manages currencies and datastore:

local currencyName = "Coins"
local levelo = "Level"
local DataStore = game:GetService("DataStoreService"):GetDataStore("Coins") --Do not change "Coins"
local DataStoreL = game:GetService("DataStoreService"):GetDataStore("Level") --Do not change "Level"
-- DO NOT MESS WITH THE DATASTORE NAMES OR ALL PLAYER DATA WILL BE LOST --


game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local level = Instance.new("IntValue")
	level.Name = levelo
	level.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	local IDL = levelo.."-"..player.UserId
	local savedDataL = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	pcall(function()
		savedDataL = DataStore:GetAsync(IDL)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
	else
		currency.Value = 50
	end
	
	if savedDataL ~= nil then
		level.Value = savedDataL
	else
		level.Value = 1
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local IDL = levelo.."-"..player.UserId
	DataStoreL:SetAsync(IDL,player.leaderstats[levelo].Value)
end)

game:BindToClose(function()
	
	--when game is ready to shutdown
	
	for i, player in pairs (game.Players:GetPlayers())do
		if player then
			player:Kick("The game is shutting down right now, your data will be saved")
		end
	end
	
	wait(5)
	
end)

btw i put "dont mess with the values" because I'm working on it with a friend

4 comments

0

Use a table. and use only one SetAsync.

0

Line 67 doesn't even save the data, what a scam

0

@TTChaos LOL

0

Don't worry that kick message won't appear either

1 answer

Well, on Line 33, you are getting the data from Datastore, but you are saving that data in DatastoreL. To fix it, you can do :

-- Line 33
savedL = DataStoreL:GetAsync(IDL)

Also, I recommend you to save multiple data in a single DataStore (using table). But, I am not including the code for it, as it may include data loss, and that you don't want.

Lemme know if it helps!

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