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
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:
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.
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
Just to check, I replace the comment with the line of code
Correct! Though, itās good to check that the datastore is actually working before you replace that
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.