Improving a Script That Spawns Explosions Randomly on the Map

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

Dont worry, i got you bro :wink: :

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

  1. Removed the separate spawnExplosion function, integrating it into the main loop.
  2. Pre-calculated half sizes and Y position to reduce computations in the loop.
  3. Simplified explosion creation by setting properties directly in the constructor.
  4. Removed unnecessary properties that were at their default values.
  5. Used task.wait() instead of wait() for more precise timing.
  6. Removed the print statement to reduce unnecessary operations.

Dear qwerty,

When a player fixes your problem, make sure to put them as “Solution”, to close the topic.

you have to tell them what you changed about the script and how it’s better

2 Likes

I know how to use the DevForum. I don’t know why you think that I don’t know what a solution is…

And as @ScrollFrameRotation has said:

1 Like

oh okay, thank you for your notice.

  1. Removed the separate spawnExplosion function, integrating it into the main loop.
  2. Pre-calculated half sizes and Y position to reduce computations in the loop.
  3. Simplified explosion creation by setting properties directly in the constructor.
  4. Removed unnecessary properties that were at their default values.
  5. Used task.wait() instead of wait() for more precise timing.
  6. 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.

1 Like

I think that you didn’t know what a solution is, because you liked my post… i just thought it worked.

Liking can be a form of thanking.

1 Like

im 200% sure he wrote that with AI

2 Likes

it’s deffo ai, the way he talks with the comments, the changelog, and the notice are extremely different

1 Like

Yeah.

Not even really how he talks in the first place.

2 Likes

he isn’t in the programmer group either. i’m checking his other posts, and it’s still the lowercase, so it cant be to be formal.

edit: deffo ai https://devforum.roblox.com/t/was-the-hunt-event-fair-in-its-timing-and-player-selection/3552049/

1 Like