I am trying to make a saving leaderstats script (from scratch so I can learn better) however, I am running into some issues. I don’t want to just copy off someone or watch a tutorial as this is something that I will need to learn how to do this eventually since it is a necessity for almost every game! So that is why I am doing this from a blank script. Here is my script and a picture of my output.
local DataStoreService = game:GetService("DataStoreService")
local LeaderstatsDataStore = DataStoreService:GetDataStore("LeaderstatsDataStore")
game.Players.PlayerAdded:Connect(function(player)
local Inventory = Instance.new("Folder")
Inventory.Name = "Inventory"
Inventory.Parent = player
local SelectedKit = Instance.new("StringValue")
SelectedKit.Name = "SelectedKit"
SelectedKit.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = player
local data, result = pcall(function()
LeaderstatsDataStore:GetAsync("Stats-"..player.UserId)
end)
if data then
Kudos.Value = data.Values.Kudos
SelectedKit.Value = data.Values.SelectedKit
else
warn(result)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local LeaderstatsSavingData = {
Values = {
["Coins"] = player.Coins.Value;
["SelectedKit"] = player.SelectedKit.Value;
}
}
local success, result = pcall(function()
return LeaderstatsDataStore:SetAsync("Stats-"..player.UserId,LeaderstatsSavingData)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end)