I’m making a mining system using Mouse.Button1Down but if there are 100+ listeners active at once whenever you click you get frame drops was wondering what the most efficient way of doing this.
local Mine = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = script.Parent.Parent
local Mineables = game.Workspace:WaitForChild("Mineables")
local Materials = require(ReplicatedStorage:WaitForChild("Materials"))
local Fruits = require(game.ReplicatedStorage:FindFirstChild("Fruits"))
local Pickaxe = require(ReplicatedStorage:WaitForChild("Fruits"))
local ClientAssets = ReplicatedStorage:FindFirstChild("ClientAssets")
local HitModule = require(script.Parent.HitModule)
local Audio = ReplicatedStorage.Audio
local Network = ReplicatedStorage.Events:FindFirstChild("CIient")
local Distance = 8
local CanMine = true
local Health = nil
local Garbage = {}
local EquippedAnimation = Instance.new('Animation');
EquippedAnimation.AnimationId = "rbxassetid://4838697852";
local humanoid = Character:FindFirstChild("Humanoid");
local EquippedAnimationTrack = humanoid:LoadAnimation(EquippedAnimation);
function Mine.Update(arg)
if arg == "UpdateRocks" then
Mine.MakeNewConnections()
end
end
function Mine.Main(Object)
local Hitbox = Object.PrimaryPart
local Holder = Player:FindFirstChild("Holding")
local press = Mouse.Button1Down:Connect(function()
if Mouse.Target == Hitbox then
local Item = Fruits[Holder.Value]
if Item.Details.ItemType == "Pickaxe" then
if Character:FindFirstChild("HumanoidRootPart") then
if humanoid.Health > 0 then
if (Character.HumanoidRootPart.Position - Hitbox.Position).Magnitude < Distance then
if CanMine then
CanMine = false
spawn(function()
local Color = Hitbox.Color
if Hitbox:FindFirstChild("Health") then
if (Hitbox.Health.Value - Item.Details.Damage) < 0 then
Mine.Effect(Object.Parent,Color)
Audio.pickup:Play()
end
end
end)
spawn(function()
EquippedAnimationTrack:Play();
Mine.Spark()
HitModule.Start(Hitbox)
Network:FireServer("HitMaterial",Hitbox)
end)
wait(0.2)
CanMine = true
end
end
end
end
end
end
end)
table.insert(Garbage,press)
end
function Mine.MakeNewConnections()
for i,v in pairs(Garbage) do
v:Disconnect()
table.remove(Garbage,i)
end
for _,Spawners in pairs(Mineables:GetChildren()) do
if #Spawners:GetChildren() > 0 then
for matsName,mats in pairs(Materials) do
if Spawners.Name == (matsName.. " Spawner") then
local ClickButton = ClientAssets:FindFirstChild("ClickButton"):Clone()
ClickButton.Parent = Spawners:FindFirstChild(matsName)
Mine.Main(Spawners:FindFirstChild(matsName))
end
end
end
end
end
Mine.MakeNewConnections()
Network.OnClientEvent:Connect(Mine.Update)
return Mine