Script running more than once

Hey! Can anyone help with my game? Currently I made a script that if you own a gamepass, it gives you a tool, but it’s running more than once, giving the player triple/double items.

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("DisableItem")

local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")

local items = {
   ["Boombox"] = script:WaitForChild("Golden Super Fly Boombox"),
  -- ["Helicopter"] = script:WaitForChild("Helicopter Powers"),
   ["TPose"] = script:WaitForChild("T-Pose"),
   ["Hecc"] = script:WaitForChild("Trail Mix of Hecc"),
   ["VM"] = script:WaitForChild("Vertical Mobility")
}


local itemID = {
	["Boombox"] = 19917060,
	--["Helicopter"] = script:WaitForChild("Helicopter Powers"),
	["TPose"] = 19916752,
	["Hecc"] = 19956254,
	["VM"] = 19956254
}

local itemsEnabled = {
	[19917060] = true,
	--["Helicopter"] = script:WaitForChild("Helicopter Powers"),
	[19916752] = true,
	[19956254] = true,
	[42] = true
}

local allowed = {
	["ShiverFrosts"] = true,
	["lmJaiz"] = true
}

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		for i,v in pairs(itemID) do
			if MPS:UserOwnsGamePassAsync(player.UserId, v) or allowed[player.Name] then
				if itemsEnabled[v] then
					for i,item in pairs(script:GetChildren()) do
						if item:FindFirstChild("ItemId").Value then
							if item["ItemId"].Value == v then
								local clone = item:Clone()
								clone.Parent = player.Backpack
								print(item.Name)
							end
						end
					end
				end
			end
		end
	end)
end)

to prevent duplicate tools in inventory add:

if item["ItemId"].Value == v then
	if not player.Backpack:FindFirstChild(item.Name) then
		local clone = item:Clone()
		clone.Parent = player.Backpack
		print(item.Name)
	end
end
  • Hi, I am not exactly sure why you’ve two of the same functionalities.
    Anyways, what’s happening here is that you’re running two in pairs foreach loops which is causing it to repeat itself as how many instances are inside itemID(table).

The first one, itemID(table) is repeating in pairs(script:GetChildren()) as many times as there are items inside itemID(table).

  • I think this is what you meant when you were writing this code.
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Event = ReplicatedStorage:WaitForChild("DisableItem");

local Players = game:GetService("Players");
local MarketplaceService = game:GetService("MarketplaceService");

local Items = {
	["Boombox"] = script:WaitForChild("Golden Super Fly Boombox"),
	["TPose"] = script:WaitForChild("T-Pose"),
	["Hecc"] = script:WaitForChild("Trail Mix of Hecc"),
	["VM"] = script:WaitForChild("Vertical Mobility")
};

local ItemIDs = {
	["Boombox"] = 19917060,
	["TPose"] = 19916752,
	["Hecc"] = 19956254,
	["VM"] = 19956254
};

local ItemsEnabled = {
	[19917060] = true,
	[19916752] = true,
	[19956254] = true,
};

local Allowed = {
	["ShiverFrosts"] = true,
	["lmJaiz"] = true
};

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		for Item, Id in pairs(ItemIDs) do
			if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Id) or Allowed[Player.Name] and ItemsEnabled[Id] then
				if ItemIDs[Item] and ItemIDs[Item]:FindFirstChild("ItemId").Value == Id then
					local clone = ItemIDs[Item]:Clone();
					clone.Parent = Player.Backpack;
				end
			end
		end
	end)
end)