Im having issues with the following datastore script. There are no errors, and when i leave the game/stop playing, it prints "Data saved" and when i start play testing again, it prints "Data Loaded", but it doesnt save or load. I dont know what is wrong and i dont know if its the save functions or load functions fault. Here is the code:
local datastoreservice = game:GetService("DataStoreService")
local datastorage = datastoreservice:GetDataStore("datastorage")
print("Variables loaded")
local function Load(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Nights = Instance.new("IntValue")
Nights.Name = "Nights"
Nights.Parent = leaderstats
print("Leaderstats created")
local data
local success, errormessage = pcall(function()
data = datastorage:GetAsync(player.UserId)
end)
print("Data load happened")
if success then
print("Data loaded")
else
warn("Error happened while loading data: "..errormessage)
end
end
local function Save(player)
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
local success, errormessage = pcall(function()
datastorage:SetAsync(player.UserId, player.leaderstats.Nights.Value)
end)
if success then
print("Data saved")
else
warn("Error happened while saving data: "..errormessage)
end
end
end
game.Players.PlayerAdded:Connect(Load)
game:BindToClose(Save)

1 comment
sorry for the late response, the script you gave me didnt work as it had a few typos and errors, however i fixed those errors and it works, so ill accept this as an answer. Thank you!