function Equip(tool, toolName)
if OwnedTools:FindFirstChild(toolName) then
for i, v in pairs(Character:GetChildren()) do
if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
v:Destroy()
else
end
end
for i, v in pairs(LocalPlayer.Backpack:GetChildren()) do
if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
v:Destroy()
else
end
end
for i, v in pairs(EquipToolsEvents:GetChildren()) do
if v:IsA("RemoteEvent")
and table.find(AllToolsNames, v.Name)
and table.find(AllToolsNames, toolName) then
v:FireServer()
print("Fired Server")
--^^^ This prints 6 times, (the amount of tools)
-- How do I make it only fire the the RemoteEvent-
-- That matched the same tool name to the RemoteEvent name?
end
end
else
NotOwnedFunction()
end
end
SERVER SIDE:
function ServerEquip(player, tool)
if player:FindFirstChild("OwnedTools") then
local OwnedTools = player.OwnedTools
if OwnedTools:FindFirstChild(tool) then
local newTool = OwnedTools[tool]
local clonedTool = newTool:Clone()
clonedTool.Parent = player.Backpack
else
warn("Player doesn't own: "..tool.Name)
end
end
end
I’m trying to make an inventory system that only allows 1 tool in the player’s backpack.
But the issue I’m having, (in the local script, 3rd for loop) the print prints 6 times, and equips me with 6 tools.
Infact, you probably don’t need the else in those two for loops:
for _, v in pairs(Character:GetChildren()) do
if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
v:Destroy()
end
end
for _, v in pairs(LocalPlayer.Backpack:GetChildren()) do
if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
v:Destroy()
end
end
Anyways, I managed to read your problem wrong, sorry.
The fix should be to create an array before the for loop and check if the tool name is in there. If not, add it in, and then fire to the server.
local loadedToolNames = []
for _, v in pairs(EquipToolsEvents:GetChildren()) do
if v:IsA("RemoteEvent")
and table.find(AllToolsNames, v.Name)
and table.find(AllToolsNames, toolName)
and (not table.find(loadedToolNames, v.Name)) then
table.insert(loadedToolNames, v.Name)
v:FireServer()
print("Fired Server")
end
end
if v:IsA("RemoteEvent") and v.Name == toolName
and table.find(AllToolsNames, v.Name)
and table.find(AllToolsNames, toolName) then
v:FireServer()
print("Fired Server")
--^^^ This prints 6 times, (the amount of tools)
-- How do I make it only fire the the RemoteEvent-
-- That matched the same tool name to the RemoteEvent name?
end
because you have it doing i, v in pairs and so it is just doing it for all of the children. You need to check if it is the child you want and if it is then run the function. You don’t need to break the loop.