CollectionService Tool clones too much

local function Update()
	
	for _, Model in CollectionService:GetTagged("Berry Bush") do
		
		local ProximityPrompt : ProximityPrompt = Model.MeshPart:FindFirstChildOfClass("ProximityPrompt")
		
		local Food = game.ServerStorage.Tools.Food.Berries
		
		if not ProximityPrompt then
			continue
		end
		
		ProximityPrompt.Triggered:Connect(function(Player)
			
			local Clone = Food:Clone()
			Clone.Parent = Player.Backpack
			
			ProximityPrompt.Enabled = false
			
			coroutine.resume(coroutine.create(function()
				
				task.wait(RegenTime * 60)
				
				ProximityPrompt.Enabled = true
				
			end))
			
		end)
		
	end
	
end

It clones the tools to probably how much berry bushes there is

2 Likes

What do you mean by “clones too much”?

1 Like

when it clones a tool, it clones a lot of it. Where it fills the entire inventory

1 Like

Do you only want the player to have 1 at most?

1 Like

I want the player to hold more berries, but from a different bush. A bush gives you one.

I removed the coroutine and made the .Enabled = false at the start to avoid any possible duplication.

Code:

--//Services
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")

--//Variables
local Food = ServerStorage.Tools.Food.Berries

--//Functions
local function Update()
	for _, Model in CollectionService:GetTagged("Berry Bush") do
		local ProximityPrompt: ProximityPrompt = Model.MeshPart:FindFirstChildOfClass("ProximityPrompt")

		if not ProximityPrompt then
			continue
		end
		
		ProximityPrompt.Triggered:Connect(function(Player)
			ProximityPrompt.Enabled = false

			local Clone = Food:Clone()
			Clone.Parent = Player.Backpack

			task.wait(RegenTime * 60)
			ProximityPrompt.Enabled = true
		end)
	end
end
1 Like

The tools duplicate still when I trigger the proximityprompt

Could you show your entire script?

local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")

local Food = ServerStorage.Tools.Food.Berries

local RegenTime = .1

local function Update()
	
	for _, Model in CollectionService:GetTagged("Berry Bush") do
		
		local ProximityPrompt: ProximityPrompt = Model.MeshPart:FindFirstChildOfClass("ProximityPrompt")
		
		if not ProximityPrompt then
			continue
		end

		ProximityPrompt.Triggered:Connect(function(Player)
			
			ProximityPrompt.Enabled = false

			local Clone = Food:Clone()
			Clone.Parent = Player.Backpack

			task.wait(RegenTime * 60)
			ProximityPrompt.Enabled = true
			
		end)
		
	end
	
end

while true do
	
	Update()
	
	task.wait(5)
	
end

Yea, the problem is that you’re calling update() over and over again, which creates tons of connections for each bush that are fired.

Fixed code:

--//Services
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")

--//Variables
local Food = ServerStorage.Tools.Food.Berries

--//Controls
local RegenTime = 60

--//Functions
local function InitializeBerryBush(bush)
	local ProximityPrompt: ProximityPrompt = bush.MeshPart:FindFirstChildOfClass("ProximityPrompt")

	if not ProximityPrompt then
		return
	end

	ProximityPrompt.Triggered:Connect(function(Player)
		ProximityPrompt.Enabled = false

		local Clone = Food:Clone()
		Clone.Parent = Player.Backpack

		task.wait(RegenTime)
		ProximityPrompt.Enabled = true
	end)
end

CollectionService:GetInstanceAddedSignal("Berry Bush"):Connect(InitializeBerryBush)

for i, berryBush in ipairs(CollectionService:GetTagged("Berry Bush")) do
	task.spawn(InitializeBerryBush, berryBush)
end

I’ve found the solution, but I’ll mark you as the solution thank you

1 Like

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