Tools duplicating twice (gamepass tools script)

I made a script in which it clones gamepass tools if the player owns a gamepass, but it’s duplicating the tools twice, I tried looking through the developer forum, but nothing seems to be working.

local mps = game:GetService("MarketplaceService")
local passFolder = game:GetService("ServerStorage"):WaitForChild("GamepassFolder")
local freeTools = {"R0TT3DC0RVSES"}

local function findPlr(plr)
	for i,v in pairs(freeTools) do
		if v == plr then
			
			return true
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		for _, v in pairs(passFolder:GetChildren()) do
			if mps:UserOwnsGamePassAsync(plr.UserId, v.ID.Value) or findPlr(plr.Name) then
				for i,x in pairs(v:GetChildren()) do
					if x:IsA("Tool") then
						if plr.Backpack:FindFirstChild(x) then
							return nil
						else
							x:Clone().Parent = plr.Backpack
						end
					end
				end
			end
		end
	end)
end)

what is in the GamepassFolder?

probaly its because u have a tool data store, a think that you can do is:

Before your clone and parents the tool for the player inventory, check if have another one, if dont have you clone and put it on the player inventory else u dont put in the player inventory

you can probably add break so the loop only runs once, so if you want to run it again, turn it into a function

 function GiveTool()
	
	for i,x in pairs(passFolder:GetChildren()) do
			if x:IsA("Tool") then
			x:Clone().Parent = plr.Backpack
            break
		end
	end
 end


plr.CharacterAdded:Connect(function(char)
		for _, v in pairs(passFolder:GetChildren()) do
			if mps:UserOwnsGamePassAsync(plr.UserId, v.ID.Value) or findPlr(plr.Name) then
GiveTool()
end
end
end)

Values with the name of the tool, the ID of the tool, and the tool itself (all inside the original value with the name of the tool)

Thank you so much!! I completely forgot about break, wasn’t even thinking haha

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