I’m a newbie to scripting, and i’ve been trying to make data saving for my fps game, but for some reason it does not save. I have Studio API services enabled, and my script is below
local DataStore = game:GetService("DataStoreService"):GetDataStore("DS1")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = folder
local data
local cashStored
local success, errorMessage = pcall(function()
data = DataStore:GetAsync(plr.UserId.."tools")
cashStored = DataStore:GetAsync(plr.UserID.."cash")
end)
if cashStored ~= nil then
cash.Value = cashStored
else
cash.Value = 500
end
if data ~= nil then
for _, toolName in pairs(data) do
local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = plr.Backpack
local newTool = tool:Clone()
newTool.Parent = plr.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId.."tools",toolsTable)
DataStore:SetAsync(plr.UserId.."cash",plr.leaderstats.Cash.Value)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserID,toolsTable)
end)
end
end)