You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I need the player to save their tools on leave and lose them on death.
What is the issue? Include screenshots / videos if possible!
When I reset/die, the tool losing function works fine, but if I leave then joined again I got them back.
Video:
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried replacing StarterGear in the savedata() function with backpack, but if they leave while having a tool equipped it wont save that tool, and I can’t save the tool in the Character since the Character is nil when a player leaves.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local toolstorage = game.ReplicatedStorage.Guns
local blacklist = {"Fists"}
local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players
local function savedata(plr, bool)
local a = {}
for _, v in pairs(plr.StarterGear:GetChildren()) do
if bool == true then
if not table.find(blacklist, v.Name) then
table.insert(a, v.Name)
data:SetAsync(plr.userId, a)
end
elseif bool == false then
if not table.find(blacklist, v.Name) then
table.remove(a, v.Name)
data:SetAsync(plr.userId, a)
end
end
end
end
players.PlayerAdded:Connect(function(plr)
local b = data:GetAsync(plr.userId)
if b then
for _, v in pairs(b) do
toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
end
end
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid", 10)
hum.Died:Connect(function()
for i,v in pairs(plr.StarterGear:GetChildren()) do
if not table.find(blacklist, v.Name) then
v:Destroy()
end
end
for i,v in pairs(plr.Backpack:GetChildren()) do
if not table.find(blacklist, v.Name) then
v:Destroy()
end
end
savedata(plr, false)
end)
end)
players.PlayerRemoving:Connect(function(plr)
savedata(plr, true)
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
local toolstorage = game.ReplicatedStorage.Guns
local blacklist = {"Fists"}
local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players
local function savedata(plr, bool)
local a = {}
for _, v in pairs(plr.StarterGear:GetChildren()) do
if bool == true then
if not table.find(blacklist, v.Name) then
table.insert(a, v.Name)
data:SetAsync(plr.userId, a)
end
elseif bool == false then
if not table.find(blacklist, v.Name) then
table.remove(a, v.Name)
data:SetAsync(plr.userId, a)
end
end
end
end
players.PlayerAdded:Connect(function(plr)
local b = data:GetAsync(plr.userId)
if b then
for _, v in pairs(b) do
toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
end
end
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid", 10)
hum.Died:Connect(function()
for i,v in pairs(plr.StarterGear:GetChildren()) do
if not table.find(blacklist, v.Name) then
v.Parent = game.ReplicatedStorage.PlrStarterGear
end
end
for i,v in pairs(plr.Backpack:GetChildren()) do
if not table.find(blacklist, v.Name) then
v.Parent = game.ReplicatedStorage.PlrBackpack
end
end
savedata(plr, false)
end)
plr.CharacterAppearanceLoaded:Connect(function()
for i,v in pairs(game.ReplicatedStorage.PlrBackpack:GetChildren()) do
v.Parent = plr.Backpack
end
for i,v in pairs(game.ReplicatedStorage.PlrStarterGear:GetChildren()) do
v.Parent = plr.StarterGear
end
end)
end)
Make a folder in replicated storage named PlrStarterGear and PlrBackpack
local toolstorage = game.ReplicatedStorage.Guns
local blacklist = {"Fists"}
local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players
local function savedata(plr, bool)
local a = {}
for _, v in pairs(plr.StarterGear:GetChildren()) do
if bool == true then
if not table.find(blacklist, v.Name) then
table.insert(a, v.Name)
data:SetAsync(plr.userId, a)
end
elseif bool == false then
if not table.find(blacklist, v.Name) then
table.remove(a, v.Name)
data:SetAsync(plr.userId, a)
end
end
end
end
players.PlayerAdded:Connect(function(plr)
local b = data:GetAsync(plr.userId)
if b then
for _, v in pairs(b) do
toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
end
end
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid", 10)
hum.Died:Connect(function()
for i,v in pairs(plr.StarterGear:GetChildren()) do
if not table.find(blacklist, v.Name) and v.Name == "Fists" then
v:Destroy()
end
end
for i,v in pairs(plr.Backpack:GetChildren()) do
if not table.find(blacklist, v.Name) and v.Name == "Fists" then
v:Destroy()
end
end
local Fists = game.StarterPack:FindFirstChild("Fists",true)
Fists:Destroy()
savedata(plr, false)
end)
end)
players.PlayerRemoving:Connect(function(plr)
savedata(plr, true)
end)