Landmine improvements

So I started making a landmine and it worked decently. Here is the video


Any suggestions? Here is the code

local hitbox = script.Parent
local debounce = false

function Explode(hit)
	local Explosion = Instance.new("Explosion")
	Explosion.Position = script.Parent.Position
	Explosion.Parent = game.Workspace
	Explosion.BlastRadius = 5
	Explosion.BlastPressure = 1000000
	script.Parent.hit:Play()
end

hitbox.Touched:Connect(function(player)
	if player.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			script.Parent.Parent.dundundun.ParticleEmitter.Enabled = true
			script.Parent.detect:Play()
			debounce = true
			task.wait(0.5)
			Explode()
			script.Parent.Parent.dundundun.ParticleEmitter.Enabled = false
			task.wait(5)
			debounce = false
		end
	end
end)

dont mind the explosion sound it was a placeholder

Hello, your code seems fine at the moment just a tiny bit problem, your function is not local

For good practices (because local is optimized) try to always add local on functions (unless you are making a class)

Note: i may not see problems with the code but others that are more experienced than me may see problems

1 Like

If I changed the script into a local script, it wouldn’t work.

Thats not what im talking about, change the function explode to local

Example

local Explode()

Ohhhh now I know what you’re talking about

1 Like

I made a few changes to your code. The main thing I changed was I restructured it so that you can have one script instead of putting multiple copies of the same script in each hitbox. Also the script can go wherever you please in the ServerScriptService or Workspace

local landmines: Folder = workspace.landmines -- the landmines'll be all be here
local blastRadius: number = 5
local blastPressure: number = 1000000

local minename: string = "landmine" -- whetever there're called

local function explode(mine, hitbox)
	local Explosion = Instance.new("Explosion")
	Explosion.Position = hitbox.Position
	Explosion.Parent = game.Workspace
	-- the next two lines make the explosion a bit more varied
	Explosion.BlastRadius = math.random(blastRadius - (blastRadius/5), blastRadius + (blastRadius/5))
	Explosion.BlastPressure = math.random(blastPressure - (blastPressure/5), blastPressure + (blastPressure/5))
	hitbox.hit:Play()
end

local function explosionCheck(hitbox: BasePart, touchpart: BasePart)
	if hitbox:GetAttribute("debounce") == true then return end
	local mine = hitbox.Parent
	if not touchpart.Parent:FindFirstChildOfClass("Humanoid") then return end
	mine.dundundun.ParticleEmitter.Enabled = true
	hitbox.detect:Play()
	hitbox:SetAttribute("debounce", true)
	task.wait(0.5)
	explode(mine, hitbox)
	mine.dundundun.ParticleEmitter.Enabled = false
	task.wait(5)
	hitbox:SetAttribute("debounce", false)
end

for i, mine in landmines:GetChildren() do
	if mine.Name ~= minename then continue end -- you could also use a class attribute to designate landmines
	local hitbox: BasePart = mine.Hitbox
	hitbox:SetAttribute("debounce", false)
	hitbox.Touched:Connect(function(touchpart)
		explosionCheck(hitbox, touchpart)
	end)
end

I wrote this quickly so there may be errors, let me know

2 Likes

Woah! That’s pretty advanced. Just saying that it’s awesome.