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

Why is my Data Store not working? It used to work, but now I can't save two things!

Hello there, I am trying to make a data store in my game so I can save the player's data when they rejoin (definition of a data store). I'm new to data stores, as this is my first time using them. This actually used to work when I was just saving the currency for the game and not survivals. Then I tried figuring out how to save two things for the next 20 minutes. I get no errors at all so I don't know how to fix it. Can somebody please tell me what's wrong?

local DataStore = game:GetService("DataStoreService"):GetDataStore("LavaData")
local CurrencyName = "Smackaroons"

--Seeing if a player is addded
game.Players.PlayerAdded:Connect(function(player)
	--leaderstats
	wait(1.5)
	local save1 = player.leaderstats.Survivals
	local save2 = player.leaderstats.Smackaroons

	local Id = "DataSaving-"..player.UserId
	
	local savedData1 = nil
	local savedData2 = nil

	--data saving
	pcall(function()
		savedData1 = DataStore:GetAsync(Id)
		savedData2 = DataStore:GetAsync(Id)
	end)
	
	--Checking if this is not nil
	if savedData1 and savedData2 ~= nil then
		save1.Value = savedData1
		save2.Value = savedData2
		
		print("Data Loaded")
		print(Id)
	else
		--If the player is new
		save1.Value = 0
		save2.Value = 0
		print("New Player")
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	--When player leaves
	local Id = "DataSaving-"..player.UserId
	DataStore:SetAsync("DataSaving-"..player.UserId,{player.leaderstats.Survivals.Value,player.leaderstats.Smackaroons.Value})
end)

game:BindToClose(function()
	--When the last player in the server leaves and the server is empty, to save it when server shuts down
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This server is shutting down, join any other server or wait")
		end
	end

	wait(5)
end)

2 answers

savedData1 is just the same as savedData2. You are not loading two different "pieces of" data but rather the same one data from one key regardless.

However, you're already saving data in the form of a table so you just need to index the data inside it when you load the data.

Consider calling GetAsync for your data which will return to you that table, then you index the data in it. You're using an array so the two pieces of data you have can be indexed with [1] and [2] (data[1], data[2]).

local DataStore = game:GetService("DataStoreService"):GetDataStore("LavaData")
local CurrencyName = "Smackaroons"

--Seeing if a player is addded
game.Players.PlayerAdded:Connect(function(player)
    --leaderstats
    wait(1.5)
    local save1 = player.leaderstats.Survivals
    local save2 = player.leaderstats.Smackaroons

    local Id = "DataSaving-"..player.UserId

    local savedData1 -- automatically nil if you don't assign it to anything
    local savedData2

    --data saving
    pcall(function()
	local data = DataStore:GetAsync(Id)
	if data == nil then
		savedData1, savedData2 = nil, nil
		print("New Player")
		return -- stop the function from continuing, a guard clause (search this on dev forum to know what it is)
	end
        savedData1, savedData2 = data[1], data[2] -- Survivals.Value, Smackaroons.Value
    end)

    --Checking if this is not nil
    if savedData1 and savedData2 ~= nil then
        save1.Value = savedData1
        save2.Value = savedData2

        print("Data Loaded")
        print("Data Key/ID: "..Id)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    --When player leaves
    local Id = "DataSaving-"..player.UserId
    DataStore:SetAsync("DataSaving-"..player.UserId,{player.leaderstats.Survivals.Value,player.leaderstats.Smackaroons.Value})
end)

game:BindToClose(function()
    --When the last player in the server leaves and the server is empty, to save it when server shuts down
    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("This server is shutting down, join any other server or wait")
        end
    end

    wait(5)
end)

Also I noticed you just refer to "Smackaroons" yet have a variable that is equal to that which you could instead have used to make the code better for customization by changing that variable. You can easily do this by replacing .Smackaroons with [CurrencyName] instead.

Instead, I'll be using tables to save your data.

local DataStore = game:GetService("DataStoreService"):GetDataStore("LavaData")

--Seeing if a player is addded
game:GetService("Players").PlayerAdded:Connect(function(player) Always use :GetSerivce()
    local folder = Instance.new("Folder", player) --Make leaderstats folder
    folder.Name = "leaderstats"
    local savea = Instance.new("NumberValue", folder)
    savea.Name = "Survivals"
    local saveb = Instance.new("NumberValue", folder)
    saveb.Name = "Smackaroons"

    local Id = "DataSaving-"..player.UserId

    local savedData = nil

    --data saving
    pcall(function()
        savedData = DataStore:GetAsync(Id)
--What? You're getting data off the same datastores?
    end)

    --Checking if this is not nil
    if savedData ~= nil then
        for i,v in pairs(savedData) do
		if i == savea.Name then
			savea.Value = v
		elseif i == saveb.Name then
			saveb.Value == v
		end
	end

        print("Data Loaded")
        print(Id)
    else
        --If the player is new
        savea.Value = 0
        saveb.Value = 0
        print("New Player")
    end

end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
    local Id = "DataSaving-"..player.UserId
    pcall(function()
    	DataStore:SetAsync("DataSaving-"..player.UserId,			 
        {player.leaderstats.Survivals.Value,player.leaderstats.Smackaroons.Value})
    end)
end)

game:BindToClose(function()
local Id = "DataSaving-"..player.UserId
    pcall(function()
    	DataStore:SetAsync("DataSaving-"..player.UserId,			 
        {player.leaderstats.Survivals.Value,player.leaderstats.Smackaroons.Value})
    end)
    wait(5)
end)

To sum it up your code is a bit messy, but no worries. Although this is a bit far from an advanced datastore you're going in the right direction. I am saving datas in tables and loading data from tables. Keep in mind saving data via leaderstats is bad. You should have 2 seperate values. One for saving, and one for the leaderstats which are always the same values. For bind to close save their data, instead of kicking. I rushed editing this script, please let me know of errors and I'll be happy to help! Sorry if this didn't work.

3 comments

0

This was helpful, but it did not work

0

I didn't get any errors though, strange

0

Put multiple print function in some places. Let me know where it is not printing

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