For some reason, my datastore doesn’t work, I tried to see if it something wrong with playerleaving but it not printing anything And it still says that there no data.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestPlayerData = DataStoreService:GetDataStore("TestPlayer1")
local function PlayerAdded(LocalPlayer)
local Leaderstats = Instance.new("Folder",LocalPlayer)
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue",Leaderstats)
Money.Name = "Money"
local Gems = Instance.new("IntValue",Leaderstats)
Gems.Name = "Gems"
local DataVersion = Instance.new("IntValue",LocalPlayer)
DataVersion.Name = "DataVersion"
local Data = nil
local success,errormessage = pcall(function()
Data = TestPlayerData:GetAsync(LocalPlayer.UserId)
end)
if success then
if Data then
print(LocalPlayer.Name .. "Data has been Loaded")
Money.Value = Data.Money
Gems.Value = Data.Gems
DataVersion.Value = Data.DataVersion
else
print("nill data :)")
return {
Money = Money.Value;
Gems = Gems.Value;
DataVersion = DataVersion.Value;
};
end
end
end
local function PlayerLeaveing(LocalPlayer)
local Leaderstats = LocalPlayer.leaderstats
local success,errormessage = pcall(function()
TestPlayerData:UpdateAsync(LocalPlayer.UserId, function(OldData)
if OldData.DataVersion == LocalPlayer.DataVersion then
return {
Money = Leaderstats.Money.Value;
Gems = Leaderstats.Gems.Value;
DataVersion = LocalPlayer.DataVersion.Value + 1;
}
else
return nil
end
end)
end)
if success then
print(" Save Data")
else
warn(errormessage)
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaveing)
This is a script inside serverscriptservice and Yes API services is turn on.
Is Studio Access to API Services on? It’s in Game Settings > Security > Enable Studio Access to API Services. It has to be on in order for Data Stores (and other services) to work.
instead of “return nil” set a default value instead
“OldData.DataVersion” is nil and won’t equal the LocalPlayers DataVersion since “OldData.DataVersion” is nil(Assuming that LocalPlayer.DataVersion) is a Int/Number Value
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestPlayerData = DataStoreService:GetDataStore("TestPlayer1")
local function PlayerAdded(LocalPlayer)
local Leaderstats = Instance.new("Folder",LocalPlayer)
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue",Leaderstats)
Money.Name = "Money"
local Gems = Instance.new("IntValue",Leaderstats)
Gems.Name = "Gems"
local DataVersion = Instance.new("IntValue",LocalPlayer)
DataVersion.Name = "DataVersion"
local Data = nil
local success,errormessage = pcall(function()
Data = TestPlayerData:GetAsync(LocalPlayer.UserId)
end)
if success then
if Data then
print(LocalPlayer.Name .. "Data has been Loaded")
Money.Value = Data.Money
Gems.Value = Data.Gems
DataVersion.Value = Data.DataVersion
else
print("nill data :)")
return {
Money = Money.Value;
Gems = Gems.Value;
DataVersion = DataVersion.Value;
};
end
end
end
local function PlayerLeaveing(LocalPlayer)
local Leaderstats = LocalPlayer.leaderstats
local success,errormessage = pcall(function()
TestPlayerData:UpdateAsync(LocalPlayer.UserId, function(OldData)
if OldData.DataVersion == LocalPlayer.DataVersion.Value then
return {
Money = Leaderstats.Money.Value;
Gems = Leaderstats.Gems.Value;
DataVersion = LocalPlayer.DataVersion.Value + 1;
}
else
return { -- deafult.
Money = 0;
Gems = 0;
DataVersion = 0;
}
end
end)
end)
if success then
print(" Save Data")
else
warn(errormessage)
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaveing)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestPlayerData = DataStoreService:GetDataStore("TestPlayer1")
local function PlayerAdded(LocalPlayer)
local Leaderstats = Instance.new("Folder",LocalPlayer)
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue",Leaderstats)
Money.Name = "Money"
local Gems = Instance.new("IntValue",Leaderstats)
Gems.Name = "Gems"
local Data = nil
local success,errormessage = pcall(function()
Data = TestPlayerData:GetAsync(LocalPlayer.UserId.."-Gems")
end)
if success then
LocalPlayer.leaderstats.Gems.Value = data
else
warn(errormessage)
end
end
local function PlayerLeaveing(LocalPlayer)
local success,errormessage = pcall(function()
DataStore:SetAsync(LocalPlayer.UserId.."-Gems", LocalPlayer.leaderstats.Gems.Value)
end)
if success then
print(" Save Data")
else
warn(errormessage)
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerLeaveing)
Ok so paste this code in and let me know if this works (it should). There are a few problems with your code;
You use a lot of capitalization and it gets cumbersome so I’d change that, it’s still valid tho
When saving data we don’t use UpdateAsync we use SetAsync, Remember: SetAsync basically means save data, and GetAsync basically means load data.
As far as I know you can not return something to a pcall