Hello, I’m trying to learn about datastore’s and I’m watching a tutorial on it, I’ve followed the steps and my datastore isn’t working. I’m getting no errors in the output, and I did change my data through the server.
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local success, errormessage = pcall(function()
data = DataStore1:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
-- set our data = to current cash
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local Data1 = player.leaderstats.Cash.Value
local success, errormessage = pcall(function()
DataStore1:SetAsync(playerUserId,Data1)
end)
if success then
print("DataSaved")
else
print("ThereWasAnError")
warn(errormessage)
end
print("done")
end)
Following a giude is good for learning the general process but you should still lookup the functions used. pcall returns two values, first a boolean of false if there was an error in the function ran and second the value returned by the function call. This can either be the error message if the function call failed or the result of the successful function call.
You should define the variable data so that it is not a global. Better still use return ( a lot of people misuse pcall)
local success, res = pcall(function()
return DataStore1:GetAsync(playerUserId)
end)
if success then
Cash.Value = res
else
-- get call failed
warn(res)
end
Edit (more advanced understanding of Lua)
You can also do this without creating a function
local success, res = pcall(DataStore1.GetAsync, DataStore1, playerUserId)
Edit: The above reply beat me to it. The above reply is using a better method of using pcall.
One possible reason why it isn’t working is because you aren’t properly assigning your data to a variable. Here is where I think the problem is:
local success, errormessage = pcall(function()
data = DataStore1:GetAsync(playerUserId)
end)
Here is the full code fixed( I haven’t tested this though):
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local data -- A variable to assign the data to
local success, errormessage = pcall(function()
data = DataStore1:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
-- set our data = to current cash
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local Data1 = player.leaderstats.Cash.Value
local success, errormessage = pcall(function()
DataStore1:SetAsync(playerUserId,Data1)
end)
if success then
print("DataSaved")
else
print("ThereWasAnError")
warn(errormessage)
end
print("done")
end)