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
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
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