My studio lags when it’s explodes the object for first time
Ig the problem is in Explosive:Destroy(), And idk how to fix this any help will be apreciated!
Here’s Module script:
local ExplosiveRemote = game.ReplicatedStorage.Remotes.ExplosiveRemote
local ExplosiveModule = {}
ExplosiveModule.ExplosiveTag = `Explosive`
ExplosiveModule.ExplosiveSoundId = `rbxassetid://135670616983730`
ExplosiveModule.ExplosiveDistance = 25 ^ 2
ExplosiveModule.ExplosiveForce = 7500
ExplosiveModule.Explode = function(Explosive:BasePart)
if not Explosive then return end
local IsExplosive = Explosive:HasTag(ExplosiveModule.ExplosiveTag)
if not IsExplosive then return end
local IsExploded = Explosive:GetAttribute(`IsExploded`)
if IsExploded then return end
Explosive:SetAttribute(`IsExploded`,true)
ExplosiveRemote:FireAllClients(Explosive.Position)
task.spawn(function()
for _, player in pairs(game.Players:GetPlayers()) do
if not player then continue end
local char = player.Character
if not char or not char.Parent then continue end
local hrp:BasePart = char:FindFirstChild(`HumanoidRootPart`)
if not hrp then continue end
local Direction = hrp.Position -Explosive.Position
local Distance = (hrp.Position -Explosive.Position).Magnitude ^ 2
if Distance > ExplosiveModule.ExplosiveDistance then continue end
task.spawn(function()
local hum:Humanoid = char:FindFirstChildWhichIsA(`Humanoid`)
if not hum then return end
local pushDirection = (Direction.Unit + Vector3.new(0, 0.5, 0)).Unit
local Force = pushDirection * (ExplosiveModule.ExplosiveForce)
hrp.AssemblyLinearVelocity = Vector3.zero
hrp:ApplyImpulse(Force)
hum:TakeDamage(hum.MaxHealth)
end)
end
end)
task.defer(function()
if Explosive then
Explosive:Destroy()
end
end)
end
return ExplosiveModule
And here’s local script:
local ExplosiveRemote = game.ReplicatedStorage.Remotes.ExplosiveRemote
local ExplosiveModule = require(game.ReplicatedStorage.Modules.ExplosiveModule)
local ExplosiveAtt = script.ExplosiveAtt
local ContentProvider = game:GetService("ContentProvider")
ExplosiveAtt.ExplosiveSound.SoundId = ExplosiveModule.ExplosiveSoundId
local assetsToLoad = {ExplosiveAtt.ExplosiveSound}
for _, child in ExplosiveAtt:GetChildren() do
if child:IsA(`ParticleEmitter`) then table.insert(assetsToLoad,child.Texture) end
end
task.spawn(function()
local success, err = pcall(function()
ContentProvider:PreloadAsync(assetsToLoad)
end)
if not success then
warn(err)
end
end)
local Player = game.Players.LocalPlayer
local MaxDistance = 250
ExplosiveRemote.OnClientEvent:Connect(function(ExplosivePos:Vector3)
if not ExplosivePos then return end
local Distance = (ExplosivePos - workspace.CurrentCamera.CFrame.Position).Magnitude
if Distance > MaxDistance then return end
task.defer(function()
local Clone = ExplosiveAtt:Clone()
Clone.Position = ExplosivePos
Clone.Parent = workspace
game.Debris:AddItem(Clone,7.5)
for _, particle in Clone:GetChildren() do
if particle:IsA(`ParticleEmitter`) then particle:Emit(particle:GetAttribute(`EmitCount`)) end
if particle:IsA(`Sound`) then particle:Play() end
end
end)
end)