What do you want to achieve?
I want to save player data, so that players can continue from where they left off when they rejoin the game.
What is the issue?
I wrote a script to save player data but it doesn’t save anything
What solutions have you tried so far?
I have checked the script for any errors, and I have also looked for solutions on the Developer Forum, but I have not found anything that solves my problem, I tried tutorials but nothing helped.
My script
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")
local function saveData(player)
local tableToSave = {
player.leaderstats.Turtles.Value;
player.leaderstats.Cursor.Value;
player.leaderstats.TurtlesPS.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Turtles = Instance.new("IntValue")
Turtles.Name = "Turtles"
Turtles.Parent = leaderstats
local Cursor = Instance.new("IntValue")
Cursor.Name = "Cursor"
Cursor.Parent = leaderstats
local TurtlesPS = Instance.new("IntValue")
TurtlesPS.Name = "TurtlesPS"
TurtlesPS.Parent = leaderstats
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
end)
if success and data then -- If there were no errors and player loaded the data
Turtles.Value = data[1]
Cursor.Value = data[2]
TurtlesPS.Value = data[3]
else
print("The player has no data!")
Cursor.Value += 1
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function()
task.wait(5)
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
Make sure it is inside a PCALL function and make it so that it prints the error message
then plesae send me the error code
local success, errormessage = pcall(function()
local data = dataSotre:getAsync()
end)
if success then
""Code here <==
else
print "Data not loaded because :"..errormessage
end
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")
local function saveData(player)
local tableToSave = {
player.leaderstats.Turtles;
player.leaderstats.Cursor;
player.leaderstats.TurtlesPS
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Turtles = Instance.new("IntValue")
Turtles.Name = "Turtles"
Turtles.Parent = leaderstats
local Cursor = Instance.new("IntValue")
Cursor.Name = "Cursor"
Cursor.Parent = leaderstats
local TurtlesPS = Instance.new("IntValue")
TurtlesPS.Name = "TurtlesPS"
TurtlesPS.Parent = leaderstats
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
end)
if success and data then -- If there were no errors and player loaded the data
Turtles.Value = data.Tutles
Cursor.Value = data.Cursor
TurtlesPS.Value = data.TurtelPS
else
print("The player has no data!")
Cursor.Value += 1
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function()
task.wait(5)
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
While your original code you provided is tentatively just fine, I’ve patched up a few issues and redundancies with some of the previously posted codes.
The fixes mainly contain changes to your method of “calling” data from the array you’re saving, redundancies surrounding unnecessary pcall()s and data formatting. Despite your original data structure being just fine, give the below code a shot and report back on any issues you may still encounter.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")
local function saveData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local Turtles, Cursor, TurtlesPS = leaderstats:FindFirstChild("Turtles"), leaderstats:FindFirstChild("Cursor"), leaderstats:FindFirstChild("TurtlesPS")
if Turtles and Cursor and TurtlesPS then
local tableToSave = {
["Turtles"] = Turtles.Value,
["Cursor"] = Cursor.Value,
["TurtlesPS"] = TurtlesPS.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved due to error: "..err)
end
end
end
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Turtles = Instance.new("IntValue")
Turtles.Name = "Turtles"
Turtles.Parent = leaderstats
local Cursor = Instance.new("IntValue")
Cursor.Name = "Cursor"
Cursor.Parent = leaderstats
local TurtlesPS = Instance.new("IntValue")
TurtlesPS.Name = "TurtlesPS"
TurtlesPS.Parent = leaderstats
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
end)
if success and data then -- If there were no errors and player loaded the data
Turtles.Value = data["Turtles"]
Cursor.Value = data["Cursor"]
TurtlesPS.Value = data["TurtlesPS"]
else
print("The player has no data!")
Cursor.Value += 1
end
end)
Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
If none of the provided code revisions work for you, the problem may also lie in your method of “updating” the data. How exactly are you updating the Turtles, Cursor, and TurtlesPS data? Where are these values being modified? From the looks of it, it seems that you may be updating these values on the client side and expecting the server to be aware of these changes and save them accordingly, which will never work. While testing your datasaving method, always make sure that all data changes occur on the server, and not from the client (in Roblox Studio’s client view, or through a LocalScript).
If kept all of the above points in mind, you shouldn’t run into issues any further.