You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Create a successfully saving datastore script.
-
What is the issue? Script prints “failed to load data” and “failed to save data” in the Studio environment where I am testing.
-
What solutions have you tried so far? I tried asking for help on the Roblox community discord and searched for solutions in the DevForums. I also tried a google search and nothing seems to help with my issue.
I was assuming I made a typo and I double-check the code. There was no typos and I cannot figure out how to fix this issue.
-- SETTINGS --
local money_name = "money"
local autosave_cooldown = 10
--------------
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Money")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = money_name
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.money.Value)
end)
if success then
print("Saved data")
else
print("Failed to save data")
end
end
local function autosave()
while wait(autosave_cooldown) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
game.Players.PlayerRemoving:Connect(function(player)
save(player)
end)
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.money.Value += amount
end)
spawn(autosave)