I’m currently coding a tool drop system, so when the player dies it drops any tools they have on their user. (Backpack or equipped). I have it set where anytime a new tool is added into the player, it enters the tools name into a table that is then read by the server, where it will duplicate any tools the player has when the player dies.
The Issue: It works well when there’s only one tool, however the moment there’s more than one, it reads it as such:
re.OnServerEvent:Connect(function(player, toolsinbackpack)
isdead.Value = true
if isdead.Value == true then
print('player died, dropping any items')
local toolclone = tooldir[table.concat(toolsinbackpack , ", ")]:Clone()
toolclone.Parent = workspace
toolclone.Handle.Position = workspaceplayer.Torso.Position
print(toolclone.Name .. " has been dropped")
isdead.Value = false
else
print('player is not dead!')
end
end)
Now the issue here, is that its reading both tool names as if it were just one tool. I need it to read them as two separate tools.
I’m currently using table.concat to read from the table as a string, but I don’t see another way to do this.
instead of inserting all the items in the table once the player picks up an item, run a for loop parenting the player tools to workspace whenever they die,
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
char:WaitForChild("Humanoid").Died:Connect(function()
for _, v in pairs(char:GetChildren()) do --this is so it also drops
-- what the player is currently holding when they die
if v:IsA("Tool") then
v.Parent = player.Backpack
end
end
for _, v in pairs(player.Backpack:GetChildren()) do
v.Parent = workspace
v:MoveTo(char.HumanoidRootPart.Position)
end
end)
end)
end)
edit: try adding this in a server script in the server script service, it should drop any tool you have when you die in game
It doesn’t run whenever they pickup a tool, its a while true loop constantly watching the players inventory to see if there’s a new entry.
while true do
wait()
for i, v in pairs(lplayer.Character:GetChildren(), lplayer.Backpack:GetChildren()) do
if v:IsA("Tool") then
if table.find(toolsinbackpack, v.name) then
--print('tool is already documented')
else
table.insert(toolsinbackpack, v.Name)
print('added ' .. v.Name .. ' to the table')
print(toolsinbackpack)
end
end
end
end
EDIT: The code above is running on the client, while the code in the OP is on the server.
Because down the line I’m going to be adding restrictions to what tools can be dropped, and what tools aren’t allowed to be dropped. And having the tool names in a table would make that easier to accomplish.
then add a boolvalue and name it ‘CanBeDropped’, set it to true if you want it to be dropped and then add a check in the script if it can be dropped.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
char:WaitForChild("Humanoid").Died:Connect(function()
for _, v in pairs(char:GetChildren()) do --this is so it also drops
-- what the player is currently holding when they die
if v:IsA("Tool") then
v.Parent = player.Backpack
end
end
for _, v in pairs(player.Backpack:GetChildren()) do
if v.CanBeDropped.Value then
v.Parent = workspace
v:MoveTo(char.HumanoidRootPart.Position)
end
end
end)
end)
end)
also this doesn’t work because you’re looking for "ClassicSword, Handgun’ in the ServerStorage.Items, use a for loop to get the items in the table then clone it rather than using table.concat since it turns it into item, item… when you have multiple items.
local myTable = {}--your table here
for index, value in pairs(myTable) do
--then your function for each 'value' here
--in your case maybe
local clone = game.ServerStorage.Item:FindFirstChild(value):Clone()
end
local myTable = {}--your table here
for index, value in pairs(myTable) do
--then your function for each 'value' here
--in your case maybe
local clone = game.ServerStorage.Item:FindFirstChild(value):Clone()
end
for index, value in pairs(toolsinbackpack) do
if isdead.Value == true then
print('player died, dropping any items')
local toolclone = tooldir:FindFirstChild(value):Clone()
toolclone.Parent = workspace
toolclone.Handle.Position = workspace[namedplayer].Torso.Position
print(toolclone.Name .. " has been dropped")
isdead.Value = false
else
print('player is not dead!')
end
end
inside the loop, which would make it not run again. you should put that after the loop is finished.
for index, value in pairs(toolsinbackpack) do
if isdead.Value == true then
print('player died, dropping any items')
local toolclone = tooldir:FindFirstChild(value):Clone()
toolclone.Parent = workspace
toolclone.Handle.Position = workspace[namedplayer].Torso.Position
print(toolclone.Name .. " has been dropped")
else
print('player is not dead!')
end
isdead.Value = false
end