I’m trying to make a script that will save leaderstats (coins, wins) + shop items that are bought. Sometimes data doesn’t save and sometimes the data does. This is getting really frusterating. I’ve asked for help on tons of discord servers, but they can’t seem to find any problems
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local coins = Coins.Value
game.ReplicatedStorage.Coinsgui.OnServerEvent:Connect(function(coins)
local CashLabel = plr.PlayerGui.Main.Cashlabel.Cash
while wait() do
CashLabel.Text = Coins.Value
end
end)
local playerUserId = plr.UserId
local data
local success, errormessage = pcall(function()
data = DataStore1:GetAsync(playerUserId)
end)
if data[2] ~= nil then
for _, toolName in pairs(data[2]) do
local tool = game.ServerStorage.ShopItems:FindFirstChild(toolName):FindFirstChild(toolName)
if tool:IsA("Tool") then
local NewTool = tool:Clone()
NewTool.Parent = plr.Backpack
local NewTool = tool:Clone()
NewTool.Parent = plr.StarterGear
else
local NewTool = tool:Clone()
NewTool.Parent = plr.Character.Head
end
end
end
local data
local success, errormessage = pcall(function()
data = DataStore1:GetAsync(playerUserId)
print(data)
end)
if success then
Coins.Value = data[1].Coins
Wins.Value = data[1].Wins
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
playerUserId = plr.UserId
data = {
Coins = plr.leaderstats.Coins.Value;
Wins = plr.leaderstats.Wins.Value;
}
local toolTables = {}
for _, tool in pairs(plr.StarterGear:GetChildren()) do
if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
table.insert(toolTables, tool.Name)
end
end
for _, tool in pairs(plr.Character.Head:GetChildren()) do
if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
table.insert(toolTables, tool.Name)
end
end
print(toolTables)
local success, errormessage = pcall(function()
DataStore1:SetAsync(playerUserId,{data,toolTables})
end)
if success then
print("Successfully saved data!")
print(DataStore1)
else
warn(errormessage)
end
end)
game:BindToClose(function()
for _, player in pairs (game.Players:GetPlayers()) do
data = {
Coins = player.leaderstats.Coins.Value;
Wins = player.leaderstats.Wins.Value
}
local toolTables = {}
for _, tool in pairs(player.StarterGear:GetChildren()) do
if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
table.insert(toolTables, tool.Name)
end
end
for _, tool in pairs(player.Character.Head:GetChildren()) do
if game.ServerStorage.ShopItems:FindFirstChild(tool.Name) then
table.insert(toolTables, tool.Name)
end
end
print(toolTables)
local success, errormessage = pcall(function()
DataStore1:SetAsync(playerUserId,{data,toolTables})
end)
if success then
print("Successfully saved data!")
else
warn(errormessage)
end
end
end)