Scripting helping datastore

local DataStore = game:GetService(“DataStoreService”)
local Mission = DataStore:GetDataStore(“Mission”)

game:GetService(“ReplicatedStorage”).DetectPlayer.OnServerEvent:Connect(function(x,plr)
local leaderstats = Instance.new(“Folder”,plr)
leaderstats.Name = “leaderstats”
local missioncomplate = Instance.new(“NumberValue”,leaderstats)
missioncomplate.Name = “Mission”
if Mission:GetAsync(“Mission”…plr.UserId) then
missioncomplate.Value = Mission:GetAsync(“Mission”…plr.UserId,missioncomplate.Value)
else
missioncomplate.Value = 0
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
local missioncomplate = plr.leaderstats.Mission
Mission:SetAsync(“Mission”…plr.UserId,missioncomplate.Value)
end)

my problem: Not saving.

GetAsync takes only key as the argument. Also you could use pcalls for saving and getting data.

Can you help complete the code?

Just remove the second argument from GetAsync, and use pcall() (aka. protected call) to get and save data.

1 Like
function SaveData(player)
  -- implement your saving logic here
end

game.Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
  for _, player in ipairs(game.Players:GetPlayers()) do
    SaveData(player)
  end
end)

Try this

1 Like