DataStores Help

Can you print the leaderstat values? They should be 0.

His? I am the person who made the post. And I am female. Did you read the post. I am trying to make a DataStore NOT DataStore2.

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