-
I’m trying to make a save tool script for my game, this way users don’t have to buy the item multiple times.
-
It doesn’t seem to save my tools when I leave and rejoin. Yesterday I was getting a queue issue, but it doesn’t show today but still, no tools save.
Video: https://drive.google.com/file/d/1sdgyhNSsiKJZHSP8yUnJkb-FRQPS9UMI/view?usp=sharing -
No errors are printed in output so I haven’t tried much.
Script(note that this is a server sided script):
local DataStore = game:GetService("DataStoreService"):GetDataStore("ToolStore") --Get our data store
game.Players.PlayerAdded:Connect(function(plr)--When player is added fire this function
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local gold = Instance.new("IntValue")
gold.Name = "gold"
gold.Value = 5000
gold.Parent = folder
local data -- set our data var
local success, errorMessage = pcall(function() --Make our pcall
data = DataStore:SetAsync(plr.UserId)
end)
if data ~= nil then -- Data isn't nothing then
for _, toolName in pairs(data) do --Loop through its name
local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName) -- Find our replicated storage folder
if tool then
local newTool = tool:Clone() -- Clone the tool
newTool.Parent = plr.Parent -- Parent the tool to the player.
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player) --When the player is leaving do
local toolsTable = {} --Set our tools table
for _, tool in pairs(player.Backpack:GetChildren()) do --Loop through the players backpack
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then --If the tool is found in our replicated storage folder then
table.insert(toolsTable,tool.Name) --Insert the tool name into the table
end
end
local success, errorMessage = pcall(function() --Set our pcall
DataStore:SetAsync(player.UserId, toolsTable)
end)
end)
game:BindToClose(function() -- If the player is the last one in the server then
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through players
local toolsTable = {} -- Make our tools table
for _, tool in pairs(player.Backpack:GetChildren()) do -- Loop through players backpack
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then --If tool exists
table.insert(toolsTable,tool.Name) --Insert tool into our table
end
end
local success, errorMessage = pcall(function() --Set pcall
DataStore:SetAsync(player.UserId, toolsTable)
end)
end
end)