Hello everybody! I’ve already done a lot of work with saving progress, but I still can’t understand why the game saves my progress at some points, and sometimes not.
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("test1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
--leaderstats
local Sharpening = Instance.new("IntValue")
Sharpening.Parent = leaderstats
Sharpening.Name = "Sharpening"
Sharpening.Value = 0
local Rebirths = Instance.new("IntValue")
Rebirths.Parent = leaderstats
Rebirths.Name = "Rebirths"
Rebirths.Value = 0
local Money = Instance.new("IntValue")
Money.Parent = leaderstats
Money.Name = "Money"
--Knife
local EquipKnife = Instance.new("StringValue", player)
EquipKnife.Name = "EquipKnife"
EquipKnife.Value = ""
local OwnedKnife = Instance.new("Folder", player)
OwnedKnife.Name = "OwnedKnife"
--Sharping
local Tools = Instance.new("Folder")
Tools.Parent = player
Tools.Name = "Tools"
local SharpeningUpgrd = Instance.new("StringValue")
SharpeningUpgrd.Parent = Tools
SharpeningUpgrd.Name = "SharpeningUpgrd"
SharpeningUpgrd.Value = "Stone"
local tableOfData
local success, errormessage = pcall(function()
tableOfData = datastore:GetAsync(player.UserId)
end)
if success and tableOfData then -- we know that it is a returning player whom already played this game before
for key, value in pairs(tableOfData) do
print(key)
print(value)
Money.Value = tableOfData["Money"]
Rebirths.Value = tableOfData["Rebirths"]
Sharpening.Value = tableOfData["Sharpening"]
SharpeningUpgrd.Value = tableOfData["SharpeningUpgrd"]
EquipKnife.Value = tableOfData["EquipKnife"]
OwnedKnife = tableOfData["OwnedKnife"]
for i, v in ipairs(OwnedKnife:GetChildren()) do
v.Name = tableOfData["OwnedKnifes"]
end
--OwnedKnife:GetChildren() = tableOfData ["OwnedKnifes"]
end
else -- we know that this player is new or their data got throttled (cached) or whatever you call it and have lost their data
--sets the default value for new players
Money.Value = 0
Rebirths.Value = 0
Sharpening.Value = 0
EquipKnife.Value = ""
SharpeningUpgrd.Value = "Stone"
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tableOfData = {
["Money"] = player.leaderstats.Money.Value,
["Rebirths"] = player.leaderstats.Rebirths.Value,
["Sharpening"] = player.leaderstats.Sharpening.Value,
["SharpeningUpgrd"] = player.Tools.SharpeningUpgrd.Value,
["EquipKnife"] = player.EquipKnife.Value,
["OwnedKnife"] = player.OwnedKnife,
["OwnedKnifes"] = player.OwnedKnife:GetChildren() ,
}
local success, errormessage = pcall(function()
datastore:SetAsync(player.UserId, tableOfData)
end)
end)
(The console does not give errors)
I used this script before and it worked, but now for some reason it doesn’t. That’s why I had a question:
"Is it normal that the script does not save data in roblox studio?"