Hello,
First sorry for my bad english.
I have problem with Data Saving in my game.
Data don t saving properly and loading too.
Here is the script from ServerScriptService:
--// VARIABLES //--
local FolderName = 'Numbers'
local AutoSave = true
local WaitTime = 120
local AutosaveIcon = true
local SaveOnLeaving = true
local DataStoreName = 'ShopSimulatorSHHData'
local SaveOnShutdown = true
local SayToSave = 'SaveGameData'
local DataStore = game:GetService("DataStoreService"):GetDataStore(DataStoreName)
local ShutDown = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = Instance.new("BindableEvent",game.Workspace)
event.Name = "PlayerChattedEvent"
--// FUNCTIONS //--
--// ON ENTERED //--
function OnEntered(plr)
local Numbers = ReplicatedStorage.Numbers:Clone()
Numbers.Parent = plr
wait(.3)
repeat wait(.1) until plr:FindFirstChild(FolderName)
local key = "User_"..plr.UserId
if plr[FolderName] then
local values = plr[FolderName]:GetChildren()
if DataStore:GetAsync(key) then
for i, l in pairs(values) do
if DataStore:GetAsync(key)[i] then
l.Value = DataStore:GetAsync(key)[i]
end
end
print(plr.Name.."'s Data is loaded")
else
local array2 = {}
for i, c in pairs(values) do
table.insert(array2,i,c.Value)
end
DataStore:SetAsync(key, array2)
print("Data created for "..plr.Name)
end
else
warn("Cannot find "..FolderName.." In "..plr.Name..", Did you typed the wrong name?")
end
end
--// ON CHATTED //--
function OnChatted(plr,msg)
if msg == SayToSave then
if AutosaveIcon == true then
script.Saving.Event:Fire(plr)
end
local array = {}
local key = "User_"..plr.UserId
if plr[FolderName] then
local vl2 = plr[FolderName]:GetChildren()
for i, k in pairs(vl2) do
table.insert(array,i,k.Value)
end
DataStore:SetAsync(key, array)
print("Data manually saved for "..plr.Name)
end
end
end
--// ON LEAVING //--
function OnLeaving(Lplr)
wait(.1)
if ShutDown == true then
if SaveOnLeaving == true then
local key = "User_"..Lplr.UserId
if Lplr[FolderName] then
local Larray = {}
local vl3 = Lplr[FolderName]:GetChildren()
for i, a in pairs(vl3) do
table.insert(Larray,i,a.Value)
end
DataStore:SetAsync(key, Larray)
print(Lplr.Name.."'s Data was saved while leaving")
end
else
print("Save on leaving is off")
end
end
end
--// ON SAVE //--
function OnSave()
for i, Nplr in pairs(game.Players:GetChildren()) do
if AutosaveIcon == true then
script.Saving.Event:Fire(Nplr)
end
local array = {}
local key = "User_"..Nplr.UserId
if Nplr[FolderName] then
local vl2 = Nplr[FolderName]:GetChildren()
for i, k in pairs(vl2) do
table.insert(array,i,k.Value)
end
DataStore:SetAsync(key, array)
print("Data automatically saved for "..Nplr.Name)
end
end
end
--// BIND TO CLOSE //--
game:BindToClose(function()
if SaveOnShutdown == true then
ShutDown = true
for i, Nplr in pairs(game.Players:GetChildren()) do
local array = {}
local key = "User_"..Nplr.UserId
if Nplr[FolderName] then
local vl2 = Nplr[FolderName]:GetChildren()
for i, k in pairs(vl2) do
table.insert(array,i,k.Value)
end
DataStore:SetAsync(key, array)
print("Data automatically saved for "..Nplr.Name.." Before shutdown")
end
end
else
print("SaveOnShutdown is off")
end
end)
--// PLAYER CHATTED CONNECT //--
game.Workspace.PlayerChattedEvent.Event:connect(function(plr,msg)
OnChatted(plr,msg)
end)
--// PLAYER ADDED CONNECT //--
game.Players.PlayerAdded:connect(function(plr)
OnEntered(plr)
end)
--// PLAYER REMOVING CONNECT //--
game.Players.PlayerRemoving:connect(function(Lplr)
OnLeaving(Lplr)
end)
--// AUTO SAVE //--
while wait(WaitTime) do
if AutoSave == true then
OnSave()
else
print("AutoSave is off")
break
end
end
And here is Numbers from ReplicatedStorage: ALL IS INTVALUE
Icon what show when Auto Saving is active:
CloneScript:
Circle image: