The code I am using will spawn random explosions around the baseplate of my map. I am satisfied with it.
I’m thinking of improving its overall performance.
I wanna improve it by making the script as simple as possible.
local interval = 0.95
local baseplate = workspace:FindFirstChild("Baseplate")
if not baseplate then
warn("Baseplate not found!")
return
end
local baseSize = baseplate.Size
local function spawnExplosion()
local randomX = math.random(-baseSize.X / 2, baseSize.X / 2)
local randomZ = math.random(-baseSize.Z / 2, baseSize.Z / 2)
local explosion = Instance.new("Explosion")
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.Position = baseplate.Position + Vector3.new(randomX, 1, randomZ)
print("Explosion at " .. tostring(explosion.Position))
explosion.BlastRadius = 40
explosion.BlastPressure = 560000
explosion.Parent = workspace
end
while true do
spawnExplosion()
wait(interval)
endk
Sorry if this post is kind of stupid. First topic I’ve done in this category
local baseplate = workspace:FindFirstChild("Baseplate")
if not baseplate then warn("Baseplate not found!") return end
local halfX, halfZ = baseplate.Size.X / 2, baseplate.Size.Z / 2
local baseY = baseplate.Position.Y + 1
while true do
Instance.new("Explosion", workspace).Position =
Vector3.new(
baseplate.Position.X + math.random(-halfX, halfX),
baseY,
baseplate.Position.Z + math.random(-halfZ, halfZ)
)
task.wait(0.95)
end
Removed the separate spawnExplosion function, integrating it into the main loop.
Pre-calculated half sizes and Y position to reduce computations in the loop.
Simplified explosion creation by setting properties directly in the constructor.
Removed unnecessary properties that were at their default values.
Used task.wait() instead of wait() for more precise timing.
Removed the print statement to reduce unnecessary operations.
local interval, baseplate = 0.95, workspace:FindFirstChild("Baseplate")
if not baseplate then return warn("Baseplate not found!") end
local baseSize, basePos = baseplate.Size, baseplate.Position
while task.wait(interval) do
Instance.new("Explosion"){
Position = basePos + Vector3.new(math.random(-baseSize.X/2, baseSize.X/2), 1, math.random(-baseSize.Z/2, baseSize.Z/2)),
ExplosionType = Enum.ExplosionType.NoCraters,
BlastRadius = 40,
BlastPressure = 560000,
Parent = workspace
}
end
Replaced wait() with task.wait() for better performance, removed unnecessary if checks, and used while task.wait(interval) do for readability. Also, pre-stored baseplate.Position and baseplate.Size to avoid redundant calls
if i changed some stuff which i shouldn’t have let me know.