This is a downgrade. The code @planeboy2021 has is correct. It’s called generalized iteration, and was introduced to the Luau language in 2022
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.
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!