DataStores Help

Yes they are 0, just printed them.

Datastore don’t have strong features as Datastore2

That’s not the point. It’s not prudent to learn DataStore2 before the normal one.

I know that. For the last and final time. I am trying to learn NOT trying to make an advanced game.

Can you print “data[1]” and “data[2]”? I suspect it’s because you are trying to refer to entries in the not yet existent table.

Hey Bloxrrey, I’m sorry for the wait, I had to experiment this for myself haha.

So it seemed to work when I tested in the real game first before testing in studio by publishing my work, joining the game, leaving then rejoining to see if the data changes. I’m not entirely sure why that’s the case, but in Studio it wasn’t working for me. I’m also not sure why nothing is printing either, because it isn’t printing for me either, though, data storing seems to work as I’ve experimented with it.

Here’s my code:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore1")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats

local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats

local playerUserId = "Player_"..player.UserId

--Local Data
	
local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)
	
if success and data then
	XP.Value = data[1]
	Level.Value = data[2]
	--Set the data equal to the current XP
else
	print("No Data")
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId

local data = {
	69;
	420;				
}

--[[ REPLACE WITH THIS 
	local data = {
	player.leaderstats.XP.Value;
	player.leaderstats.Level.Value
--]]

local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)
	
if success then
	print("Data saved")	
else
	print("Error in saving data")
	warn(errormessage)
end
end)

I set the data values to 69 and 420 in a table, but you can replace it later after you test the functionality. Please let me know if this works.

So it works in-game but not inside studio?

You said it there on your post.
I was getting an error but now I am not, I haven’t changed my script. I am so confused.

show me the error from your Output.

It seems to me that that is the case, I’m not entirely sure why, but I’ve been having issues like that ever since I learned datastore haha. But test it out and get back to me :slight_smile:

1 Like

It isn’t doing an error anymore as I said, so it doesn’t seem relevant. I don’t know how to get the error again.

So do you think that there might have been an issue because I didn’t check if there was data?

The problem with your script is that there might not be any data stored in data. Because this might be the first time data was called.

To counteract this, you can add an extra if statement:

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)
	
if success then
    if data then
        XP.Value = data["XP"] -- Noticed how I changed [1] to ["XP"], this is because datastore is more organized this way
        Level.Value = data["Level"]
        --Set the data equal to the current XP
    else
        local DataTable = {XP = 10, Level = 1}
        myDataStore:SetAsync(DataTable) -- If there is no data, then we will set the data
    end
end

I’ve added some modifications to your code, such as changing data[1] to data["Exp"]. This is because if you add more data later on, data can be messed up. Here’s a thread that I’ve made in the past that can explain my concerns:

1 Like

It’s efficient to check whether or not data has been established before setting any values because there could have been an error. If no data exists, values cannot be set.

However, in terms of saving data, I’m not sure why the pcall values aren’t printing haha.

1 Like

If the pcalled GetAsync() is successful, do something like -

if success then
    if data == nil then
    —Set the data forcibly as a table
    end
end

This is also another way to do it @Bloxrrey, and make sure to set the Values of XP to 10 and Level to 1 in the leaderstats if you do choose to do it this way :slight_smile:

1 Like

Just to check, I replace the comment with the line of code image

Correct! Though, it’s good to check that the datastore is actually working before you replace that :slight_smile:

1 Like

Hi, I just noticed that you used semi-colons in the data dictionary to separate entries. This is incorrect, so use commas instead. The last entry doesn’t need a comma.

1 Like

I’m biased for semi colons, they look nice lol. I was under the impression that semi colons can separate elements within a table and to end lines of code.

Understandable if you were used to other languages than RBX.Lua lol