Hi, I have an issue with my data store and it prints yes for success and prints data saving however it does not show when I rejoin. I had a touch brick which gave me 10 Molla however when I rejoined, I had no Molla.
local myDatasStore = datastore:GetDataStore("myDataStore")-- In Quotes is name of data store
game.Players.PlayerAdded:Connect(function(p)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = p
local money = Instance.new("IntValue")
money.Name = "Molla"
money.Parent = leaderstats
local playerid = "Player_"..p.UserId
print(playerid)
myDatasStore:GetAsync(playerid) -- Get data
--Load Data
local Data = nil
local success, errormessage = pcall(function()
Data = myDatasStore:GetAsync(playerid)---- Local for it to be shown for both functions
return Data
end)
if success then -- if the data is found
p.leaderstats.Molla.Value = Data
print("yes")
else
print("rip")
warn(errormessage)
-- where data is loaded into the actual game
end
end)
game.Players.PlayerRemoving:Connect(function(p)
local playerid = "Player_"..p.UserId
print(playerid)
local Data = p.leaderstats.Molla.Value --- Turning it into data
local success, errormessage = pcall(function()
myDatasStore:GetAsync(playerid, Data)-- saving the money -- Get the data already in game to save
end)
print("Passed")
if success then
print("Data saved")
else
print("no")
warn(errormessage)
end
end)
You’re not actually calling any methods on your datastore to save your information, only the GetAsync method which retrieves previously saved data. Please take a look at the following datastore methods to get a better idea of how you want to go about saving your game’s data
No problem, though be sure to read the API too next time like @mew903 mentioned above with the links too. You could’ve solved this issue with a bit of searching.
This is not exactly a fix to the issue, but I thought it was good to note something:
You are creating a local variable Data and then setting it within the pcall function.
While this works, it is not advised to do it this way.
The pcall function actually sets what you have as errormessage to what you return within the pcall function.
You should make this code
local success, data = pcall(function()
Data = myDatasStore:GetAsync(playerid)---- Local for it to be shown for both functions
return Data
end)
then data is either the save data or the error message
if success is true then data is equal to the save data
if success is false then data is equal to the error message.