Hello,
I have a module script that sorts the player’s inventory. However, whenever it is run, it causes guns to fire remote events twice. I’m not sure why this is happening, and that is my question.
The script that sorts the inventory:
function Items.SortItems(Player)
local Primaries = Items.GetAllItemsOfSlot(Player,"Primary")
local Secondaries = Items.GetAllItemsOfSlot(Player,"Secondary")
local Utility = Items.GetAllItemsOfSlot(Player,"Utility")
local Consumables = Items.GetAllItemsOfSlot(Player,"Consumable")
local BackpackItems = Player:WaitForChild("BackpackItems",20)
Utilities.ChangeParent(Player.Backpack,BackpackItems)
local EquippedItem = Player.Character:FindFirstChildOfClass("Tool")
if EquippedItem then
EquippedItem.Parent = BackpackItems
end
task.wait(0.15)
if #Primaries > 0 then
Utilities.ChangeTableParent(Primaries,BackpackItems)
task.wait(0.1)
for i,v in pairs(Primaries) do
v.Parent = Player.Backpack
end
end
task.wait(0.15)
if #Secondaries > 0 then
Utilities.ChangeTableParent(Secondaries,BackpackItems)
task.wait(0.1)
for i,v in pairs(Secondaries) do
v.Parent = Player.Backpack
end
end
task.wait(0.15)
if #Utility > 0 then
Utilities.ChangeTableParent(Utility,BackpackItems)
task.wait(0.1)
for i,v in pairs(Utility) do
v.Parent = Player.Backpack
end
end
task.wait(0.25)
if #Consumables > 0 then
Utilities.ChangeTableParent(Consumables,BackpackItems)
task.wait(0.1)
for i,v in pairs(Consumables) do
v.Parent = Player.Backpack
end
end
if EquippedItem and Player.Character then
EquippedItem.Parent = Player.Character
end
end
GetAllItemsOfSlot():
function Items.GetAllItemsOfSlot(Player,ItemSlot)
local ItemList = {}
for i,Item in pairs(Player.Backpack:GetChildren()) do
local ItemItemSlot = Item:GetAttribute("ItemSlot")
if ItemItemSlot and ItemItemSlot == ItemSlot then
table.insert(ItemList,Item)
end
end
local BackpackItems = Player:WaitForChild("BackpackItems",20)
for i,Item in pairs(BackpackItems:GetChildren()) do
local ItemItemSlot = Item:GetAttribute("ItemSlot")
if ItemItemSlot and ItemItemSlot == ItemSlot then
table.insert(ItemList,Item)
end
end
local EquippedTool = Player.Character:FindFirstChildOfClass("Tool")
if EquippedTool then
if EquippedTool:GetAttribute("ItemSlot") and EquippedTool:GetAttribute("ItemSlot") == ItemSlot then
table.insert(ItemList,EquippedTool)
end
end
return ItemList
end
Utilities.ChangeTableParent:
function Utilities.ChangeTableParent(Table,Folder2)
for i,v in pairs(Table) do
v.Parent = Folder2
end
end
I hope someone can help me solve this problem since it is an important feature of the game. Thanks for your help.