String is nil when passed through remote event

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local RepliStorage = game:GetService("ReplicatedStorage")

local player_data = DataStoreService:GetDataStore("TestData1")
local RemoveSoldItem = RepliStorage.Remotes.RemoveSoldItem

local toolsInstance = ServerStorage.Tools

local tools = {}

Players.PlayerAdded:Connect(function(client)
	client.CharacterAdded:Connect(function(plrChar)
		print(tools[client])
		tools[client] = {}
		
		local key = "client_" .. client.UserId
		local inventory = player_data:GetAsync(key) 
		print(inventory)
		
		
		
		for i, name in inventory do
			print(name)
			local tool = game:GetService("ReplicatedStorage").ItemAssets:FindFirstChild(name)
			local Backpack = client.Backpack
			print(tool)

			if tool then
				print(tool.Name)
				local Clonedtool = tool:Clone()
				Clonedtool.Name = tool.Name
				Clonedtool.Parent = Backpack
				print("tool cloned")
			else
				warn("Item could not be cloned")
			end
		end
		
		RemoveSoldItem.OnServerEvent:Connect(function(plr, toolItemName)
			

			if #tools[plr] == 0 then
				warn("No tools found for player: " .. plr.Name)
				
			end


			local TargetPosition = table.find(tools[plr], toolItemName)
			print(tools[plr])
			print(toolItemName)

			print("Current tools for player:", table.concat(tools[plr], ", "))

			if TargetPosition then
				print("Removing tool at position:", TargetPosition)
				table.remove(tools[plr], TargetPosition)
			else
				warn("Tool not found in player's inventory")
			end
		end)

		
	end)
	

end)

Players.PlayerRemoving:Connect(function(client)

	local key = "client_" .. client.UserId
	tools[client] = { }

	local Character = client.Character
	local Backpack = client.Backpack
	if Character then
		for X, item in Character:GetChildren() do
			if not item:IsA("Tool") then continue end

			table.insert(tools[client], item.Name)
		end
	end

	for _, item in ipairs(client.Backpack:GetChildren()) do
		if item:IsA("Tool") then
			print(item.Name)
			table.insert(tools[client], item.Name)
		else
			warn(item.Name .. " is not a Tool")
		end
	end

	print(tools[client])
	player_data:UpdateAsync(key, function(prev)
		
		return tools[client]
		
	end)
end)

Good lord. Remove that embedded RemoteEvent.OnServerEvent handler immediately

Why?


I see nothing that inserts into the player’s cached inventory during their lifetime in the server. You’re only doing this when the player disconnects, which has no relevance to your RemoveSoldItem event

What do I substitute with toolItemName?

local TargetPosition = table.find(tools[client], toolItemName)

Each time a player enters your game and (re)spawns, you will clone the event listener to RemoveSoldItem.OnServerEvent. This can lead to serious adverse collateral damage as you begin to duplicate your response to the event. It is also a greedy memory leak, which generally has the potential to seriously impact performance as the game continues on. There is zero necessity for your event listener to be characterized this way—it doesn’t even use its immediate upvalues

Move the event listener into the main scope away from Players.PlayerAdded

Like this? Also look at my previous post.

Players.PlayerAdded:Connect(function(client)
	client.CharacterAdded:Connect(function(plrChar)
		print(tools[client])
		tools[client] = {}
		
		local key = "client_" .. client.UserId
		local inventory = player_data:GetAsync(key) 
		print(inventory)
		
		
		
		for i, name in inventory do
			print(name)
			local tool = game:GetService("ReplicatedStorage").ItemAssets:FindFirstChild(name)
			local Backpack = client.Backpack
			print(tool)

			if tool then
				print(tool.Name)
				local Clonedtool = tool:Clone()
				Clonedtool.Name = tool.Name
				Clonedtool.Parent = Backpack
				print("tool cloned")
			else
				warn("Item could not be cloned")
			end
		end
		
		-- Here
			

			if #tools[client] == 0 then
				warn("No tools found for player: " .. client.Name)
				
			end


			local TargetPosition = table.find(tools[client], toolItemName)
			print(tools[client])
			print(toolItemName)

			print("Current tools for player:", table.concat(tools[plr], ", "))

			if TargetPosition then
				print("Removing tool at position:", TargetPosition)
				table.remove(tools[client], TargetPosition)
			else
				warn("Tool not found in player's inventory")
			end
		end)

		
	
	

end)

Use pairs(inventory) instead of inventory

This is a downgrade. The code @planeboy2021 has is correct. It’s called generalized iteration, and was introduced to the Luau language in 2022

1 Like

What do I substitute with toolItemName?

local TargetPosition = table.find(tools[client], toolItemName)

Nothing. Why do you think you need to change that? Also, your amendment to the RemoveSoldItem.OnServerEvent event handler is incorrect. I doubt you want to remove the event handler in its entirety. Once again, do not implement the event handler ANYWHERE inside of your Players.PlayerAdded event handler.

1 Like

I misunderstood you sorry.


As I said earlier, your problem is that tools[player] is never appropriately populated. When you’re loading the tools into the player’s backpack, add the name of the tool to tools[client] in the same way you do in your Players.PlayerRemoving event handler.

However, the code you’ve presented so far seems to have no relevant use for the tools table. Consider eliminating it from your script

First of all tools[plr] is nil when printed
Nothing gets run after this part.

if #tools[plr] == 0 then
		warn("No tools found for player: " .. plr.Name)
		return
	end

Section of code:

RemoveSoldItem.OnServerEvent:Connect(function(plr, toolItemName)


	if #tools[plr] == 0 then
		warn("No tools found for player: " .. plr.Name)
		return
	end


	local TargetPosition = table.find(tools[plr], toolItemName)
	print(tools[plr])
	print(toolItemName)

	print("Current tools for player:", table.concat(tools[plr], ", "))

	if TargetPosition then
		print("Removing tool at position:", TargetPosition)
		table.remove(tools[plr], TargetPosition)
	else
		warn("Tool not found in player's inventory")
	end
end)

What do you mean eliminate the tools table?

tools variable is a table of the player’s backpack.

and why are you trying to find player in tools table?

it has to be something like this:

if #tools == 0 then
	warn("No tools found for player: " .. plr.Name)
	return
end

This is a server script.


Also is the return the reason why nothing gets runned after that?

yes obviously. It ends the function execution and provides the result if there is

I solved it.

I moved it to the playerRemoving function.

Players.PlayerRemoving:Connect(function(client)

	local key = "client_" .. client.UserId
	tools[client] = { }

	local Character = client.Character
	local Backpack = client.Backpack
	if Character then
		for X, item in Character:GetChildren() do
			if not item:IsA("Tool") then continue end

			table.insert(tools[client], item.Name)
		end
	end

	for _, item in ipairs(client.Backpack:GetChildren()) do
		if item:IsA("Tool") then
			print(item.Name)
			table.insert(tools[client], item.Name)
		else
			warn(item.Name .. " is not a Tool")
		end
	end
	

	RemoveSoldItem.OnServerEvent:Connect(function(plr, toolItemName)



		local TargetPosition = table.find(tools[plr], toolItemName)
		print(tools[plr])
		print(toolItemName)

		print("Current tools for player:", table.concat(tools[plr], ", "))

		if TargetPosition then
			print("Removing tool at position:", TargetPosition)
			table.remove(tools[plr], TargetPosition)
		else
			warn("Tool not found in player's inventory")
		end
	end)

	print(tools[client])
	player_data:UpdateAsync(key, function(prev)
		
		return tools[client]
		
	end)
	
end)

Now I am going to remove a few print statements!