I want to make the player save their tools after death by getting the names of the tools and putting them on a table. If the player is spawned, it will clone the tools from a folder and, with the help of the table, it will find the tools that need to be cloned and added to the player’s back pack.
I used up all of my brain cells (even though I do not have brain cells anymore XD) to come up with a script in the server script service, but it doesn’t work.
I tried searching on Google and YouTube, but nothing seemed relevant to what I was doing.
local toolfolder =game.ReplicatedStorage.ToolsFolder -- location where the folder of tool is
local toolstored = {} -- tables
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local tableloop = true -- this will prevent loops later on the script
local hum = char:FindFirstChild("Humanoid")
for i, toolstored in pairs(toolstored) do
toolfolder[toolstored]:Clone().Parent = plr.Backpack
toolfolder[toolstored]:Clone().Parent = plr.StarterGear
tableloop = true
end
hum.HealthChanged:Connect(function()
if hum.Health <= 0 then
if tableloop == true then
for i, v in pairs(plr.Backpack:GetChildren()) do
if v:IsA("Tool") then
table.insert(toolstored, v.Name)
end
tableloop = false
for index, value in pairs(toolstored) do
print(index, value)
end
table.clear(toolstored)
end
end
end
end)
end)
end)
This script would only work if there was one player in the server, add more and you’ll see that every player will respawn with each other’s tools because your using one table for every player.
Also, what happens when you die with tools and then respawn?
I’m going to make the script work for individual players. As for the question, the player will later respawn but without the tool since the script is not working.
I will handle the script that only works for individual players, but for now I’m just having trouble understanding why my script is not cloning the tools from the “tool folder” that the table listed to be cloned (I’m assuming the “table” listed the names of the tools).
Also for handling with individuals, I suggest you add a table within a table for each person. So add a table inside the toolstored table. As of now, each player are getting the same tool??
May I ask about the table within the table for each person and how it works? And to answer your question, no, whatever that player had recently in their backpack will reappear in their backpack when they respawn. My aim here is to do something that is similar to “keepInventory” from Minecraft… if you know that game.
The table for each person means that for every player that joins, you create a table as a value for the toolstored table and the key being the player, so that each player has seperate tool saves.
Humanoid.Died is an event that you could use over healthchanged if your only detecting if a player dies. One more thing im confused on is why are you using a table loop bool, and then clearing the entire table?
local Players = game:GetService("Players")
local ToolsFolder = --path to your tools folder
local ToolSaves = {} --dict for player tools
local function PlayerAdded(player: Player)
ToolSaves[player] = {}--create a sub table for their inventory
player.CharacterAdded:Connect(function(char)
for _, toolName in ToolSaves[player] do
local tool = ToolsFolder:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = player.Backpack
end
end
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
table.clear(ToolSaves[player])--clear existing tools
for _, tool in player.Backpack:GetChildren() do --get tools in backpack
if not tool:IsA("Tool") then continue end
table.insert(ToolSaves[player], tool.Name)
end
local charTool = char:FindFirstChildWhichIsA("Tool") --in case player is equipping a tool while dying
if charTool then table.insert(ToolSaves[player], charTool.Name) end
end)
end)
end
Players.PlayerAdded:Connect(PlayerAdded)
for _, player in Players:GetPlayers() do --in case players joined the game before script ran
task.spawn(PlayerAdded, player)
end
Players.PlayerRemoving:Connect(function(player) --remove player save
if ToolSaves[player] then
ToolSaves[player] = nil
end
end)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(p)
local Temp = Instance.new("Folder")
Temp.Name = p.UserId
Temp.Parent = script
p.CharacterAdded:Connect(function(ch)
if #Temp:GetChildren() > 0 then
for i,v in pairs(Temp:GetChildren()) do
v.Parent = p.Backpack
end
end
ch.Humanoid.Died:Connect(function()
for i,v in pairs(p.Backpack:GetChildren()) do
v:Clone().Parent = script:WaitForChild(tostring(p.UserId))
end)
end)
end)
That shouldn’t be possible, since Backpack is the explicit name for the folder used in storing tools for a player. I’m not sure why it’s warning but you should try running the game and seeing what happens.