Tools are only saving when the player leaves the game. This is bad because If I was to shutdown the game they would loose tools, and If they were to reset their character their tools will not save too. Since I am new to scripting, does anyone have tips on how to write more into this script so they save periodically instead of just leaving.
Here is the script -
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore = dataStoreService:GetDataStore(“BackpackSave”)
game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync(“User-”…player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall (function()
local toolsSave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
if tool then
table.insert(toolsSave,tool.Name)
end
end
dataStore:SetAsync(“User-”…player.UserId,toolsSave)
end)
end)
For your first issue, shutting down the server involves players leaving, so playerremoving would be called. You should add a bind to close so that it gives the server more time to save before the server closes.
For your second concern, you could always just store what tools the player has when they died, and then when they respawn, just give them back the tools they had.
The server shuts down a little bit later in a normal game, in the studio however the server closes as soon as the player leaves, the BindToClose is only optional for the studio
You can use DataStoreService to save a table with the index/key being the tool name then the value as a bool value (true or false) to see if they own it. For the issue of the player dying then re-spawning you could create a separate folder or table to loop through when they die and clone those owned items to the player’s backpack.
(for future refrence use three ``` characters to form a script box)
local DataStoreService = game:GetService("DataStoreService")
local BackpackStore = DataStoreService:GetDataStore("BackpackStore")
game.Players.PlayerAdded:Connect(function(player)
local ownedItems = Instance.new("Folder", player) -- create a "death folder" to loop through when the player dies
ownedItems.Name = "OwnedItems"
local Data
local success, err = pcall(function()
Data = BackpackStore:GetAsync("User-"..player.UserId) -- get the data
end)
if Data then -- if Data ~= nil works to (checks if there is actually data)
local items = game.ReplicatedStorage:WaitForChild("Items") -- get the items folder
for i, v in pairs(Data) do -- loop through data since it's a table
if v then -- same as if v == true
if items:FindFirstChild(i) then -- i is equal to the tool name
-- we'll clone the tool twice, one for the player one for the death folder
local playerTool = items:FindFirstChild(i):Clone()
local ownedTool = items:FindFirstChild(i):Clone()
playerTool.Parent = player:WaitForChild("Backpack")
ownedTool.Parent = ownedItems
end
end
end
end
player.CharacterAdded:Connect(function(character) -- character added function for when the character loads
local humanoid = character:WaitForChild("Humanoid") -- get the humanoid
humanoid.Died:Connect(function() -- humanoid.Died function when they die
wait(game.Players.RespawnTime + 1) -- wait the respawn time of the Players service
for _, tool in pairs(ownedItems:GetChildren()) do -- loop through the death folder and set the parents
tool:Clone().Parent = player:WaitForChild("Backpack")
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local savedTools = {} -- default table
for i, v in pairs(player:WaitForChild("OwnedItems"):GetChildren()) do
savedTools[v.Name] = true -- same as doing table.insert
end
local success, err = pcall(function()
BackpackStore:SetAsync("User-"..player.UserId, savedTools) -- save the table
end)
if not success then warn(err) end -- check if the data failed to save
end)
If I made any mistakes in this code example let me know There are MANY other ways of doing this, I just gave a basic solution
Do the tools at least go into the “OwnedItems” folder? Do the tools go back into the backpack after death? Are there any errors? From what I typed it should work. Do me a favor after checking the above insert the following into the player removing for loop,
for i, v in pairs(player:WaitForChild("OwnedItems"):GetChildren()) do
print(v.Name)
saveTools[v.Name] = true
print(saveTools[v.Name])
end
I use print statements to check if code is working so put those there to see if the table is inserting the values