Issue with reading multiple table entries?

Afternoon everyone!

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:
image

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.

I hope you guys can help, and thanks in advanced!

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

2 Likes

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.

1 Like

why do you need to put what they have in a table?

1 Like

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.

1 Like

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)
1 Like

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.

2 Likes

Hey there, sorry. Didn’t see you responded.

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

How would I do this? Because trying to read the table puts each entry like this:

[1] = “Classic Sword”
[2] = “Handgun”

1 Like

do

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
1 Like

Unfortunately, this didn’t work. It still only dropped the first item in the table not both.

1 Like

what script did you try?

this or

this

1 Like
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

This one

1 Like

oh did you position it to the player and also parent it to workspace, since i forgot to add it

1 Like

This is what I have:

	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

try printing the whole table before running the loop

Yeah, it prints showing that both items are entered in the table.

wait, i just realized, you added

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
1 Like

Yep! That fixed it, thanks again man. I appreciate you.

1 Like