How can i include bind to close in this script?
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("IIII")
local toolsFolder = game.ServerStorage.ShopItems
game.Players.PlayerAdded:Connect(function(plr)
local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
for i, toolSaved in pairs(toolsSaved) do
if toolsFolder:FindFirstChild(toolSaved) then
toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear
end
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
The code you have should not yield, so you can put any new code at the bottom for binding something to close.
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("IIII")
local toolsFolder = game.ServerStorage.ShopItems
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(plr)
local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
for i, toolSaved in pairs(toolsSaved) do
if toolsFolder:FindFirstChild(toolSaved) then
toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear
end
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
local toolsOwned = {}
game:BindToClose(function(plr)
if not RunService:IsStudio() and #Players:GetPlayers() > 1 then
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(plr.UserId .. "-tools", toolsOwned)(player)
end
end
end)
Like this?