A certain function causing remote events to fire twice

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.

Point it out if I missed it please, but I don’t see anywhere a RemoteEvent is being fired. Can you send that code and the surrounding code?

Here, however I’m not sure it’s actually related to the script itself.

local Remote = game.ReplicatedStorage.GunsShoot.ShotgunShoot
local Tool = script.Parent
local Ammo = Tool:WaitForChild("Ammo")
local Player = game.Players.LocalPlayer
local debounce = false
local AmmoGui = Player.PlayerGui:WaitForChild("Ammo",8)
local UserInputService = game:GetService("UserInputService")
local AimCursor = Player.PlayerGui:WaitForChild("AimCursors"):WaitForChild("Cursor2")
Tool.Activated:Connect(function()
	local Mouse = Player:GetMouse()
	local MousePos = Mouse.Hit.Position

	if debounce == false and Player.Character and Player.Character.Humanoid.Health > 0 and Tool:WaitForChild("Ammo",8).Value > 0 and Mouse.Hit then
		debounce = true
		Remote:FireServer(MousePos)
		print(script.Parent.Name)
		print(script.Parent.Parent.Name)
		task.wait(1)
		Tool:FindFirstChild("ReadySound",true):Play()
		debounce = false
	else if debounce == false and Player.Character and Player.Character.Humanoid.Health > 0 and Tool:WaitForChild("Ammo",8).Value <= 0 then
			local Reload = game.ReplicatedStorage.GunsReload.ShotgunReload
			Reload:FireServer(Tool)
		end
	end
end)

It seems like changing this:

Utilities.ChangeParent(Player.Backpack,BackpackItems)

to this:

for i,v in pairs(Player.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			v.Parent = BackpackItems
			task.wait(0.1)
		end
	end

Solved the problem. This is probably because of the added task.wait()
I’m not sure why this is happening, but that seemed to fix it.
However, if anyone knows the reason why this is happening, please tell me.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.