(I posted this for the second time as last time the post was abandoned and it did not help.)
Hello developers/Scripters.
I am experiencing an issue that the Data Store does not save the players data. I have tried both GetAsync() and SetAsync() when saving but it did not fix the problem at all.
I even re-made the code and replaced it with one of a tutorial completely which worked every single last time but still, did not work.
Here is the entire script:
local DataStoreService = game:GetService("DataStoreService")
local GameDataStore = DataStoreService:GetDataStore("GameSaveData")
local function saveData(player)
local tableToSave = {
player.leaderstats["Total Clicks"].Value,
player.leaderstats.Clicks.Value,
player.leaderstats.Level.Value,
player.leaderstats.Multiplier.Value,
}
local success, err = pcall(function()
GameDataStore: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(plr)
local playerid = plr.UserId
local data = GameDataStore:GetAsync(playerid)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local TotalClicks = Instance.new("IntValue")
TotalClicks.Value = 0
TotalClicks.Name = "Total Clicks"
TotalClicks.Parent = folder
local Clicks = Instance.new("IntValue")
Clicks.Value = 0
Clicks.Name = "Clicks"
Clicks.Parent = folder
local Level = Instance.new("IntValue")
Level.Value = 1
Level.Name = "Level"
Level.Parent = folder
local Multiplier = Instance.new("IntValue")
Multiplier.Value = 1
Multiplier.Name = "Multiplier"
Multiplier.Parent = folder
local data
local success, err = pcall(function()
data = GameDataStore:GetAsync(plr.UserId)
end)
if success and data then
TotalClicks.Value = data[1]
Clicks.Value = data[2]
Level.Value = data[3]
Multiplier.Value = data[4]
else
print("The player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
saveData(plr)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)
Thanks to anyone who can help me figure out how to fix it with a proper explanation!
(I need to get this working within 5 days as it is a fiverr order I have to get done and I don’t want to delay it again, risking a bad review.)
Edit: I also looked at other posts which did not seem to help.
Right, I just tested this code, and it worked perfectly fine for me. Are you able to provide more information on where the script is located? and is datastore access on? Tell me as much information as possible.
I do have API Services on, and the script is located in ServerScriptService. I will do more testing on the server side instead of client side as I only interacted with the values over local scripts.
Hello! I’m Miles, a developer that’s pretty experienced with datastores. I am not familiar with your format using datastores, so I’ll provide a fixed version of my own. Make sure to put this script in ServerScriptService since datastores can only work using Server-Sided scripts. You could also use remote events if you want to change data from a client. Remember to also enable API Services so your code will work. Here is the provided code:
local data = DataStoreService:GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Clicks = Instance.new("IntValue")
Clicks.Parent = leaderstats
Clicks.Name = "Clicks"
local Level = Instance.new("IntValue")
Level.Parent = leaderstats
Level.Name = "Level"
local Multiplier = Instance.new("IntValue")
Multiplier.Parent = leaderstats
Multiplier.Name = "Multiplier"
local dataTable = {
["Clicks"] = 0,
["Level"] = 1,
["Multiplier"] = 1
}
local success, err = pcall(function()
dataTable = data:GetAsync("player_"..player.UserId) or dataTable
end)
if success then
Clicks.Value = dataTable["Clicks"]
Level.Value = dataTable["Level"]
Multiplier.Value = dataTable["Multiplier"]
else
Clicks.Value = 0
Level.Value = 1
Multiplier.Value = 1
end
local function updateValue(player)
dataTable.Clicks = Clicks.Value
dataTable.Level = Level.Value
dataTable.Multiplier = Multiplier.Value
data:SetAsync("player_"..player.UserId, dataTable)
end
Clicks:GetPropertyChangedSignal("Value"):Connect(function()
updateValue(player)
end)
Level:GetPropertyChangedSignal("Value"):Connect(function()
updateValue(player)
end)
Multiplier:GetPropertyChangedSignal("Value"):Connect(function()
updateValue(player)
end)
leaderstats.Parent = player
end)