How to I make my mine explode when any part touches it?

How to I make my mine explode when ANY part touches it?
(this doesn’t include terrain)

Here is what I have so far:

local mine = script.Parent
local minePosition = mine.CFrame.Position
local sound = script.Parent.BoomSound
local DEBOUNCE = false
mine.Transparency = 0.9

--[[
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
	humanoid.Health = 0
	sound:Play()
end
]]

mine.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not DEBOUNCE then
		DEBOUNCE = true
		
		hit.Parent.Humanoid.Health = 0
		
		sound:Play()
		local boom = Instance.new("Explosion")
		boom.ExplosionType = Enum.ExplosionType.NoCraters
		boom.BlastPressure = 300000
		boom.BlastRadius = 10
		boom.Parent = mine
		boom.Position = minePosition
		boom.Visible = true
		boom.Hit:Connect(function(part: BasePart, _)
			part.Anchored = (part == mine) or false
		end)
		
		local light = Instance.new("PointLight")
		light.Parent = mine
		light.Brightness = 10
		light.Range = 12
		light.Color = Color3.new(0.942306, 0.0528725, 0)
		light.Enabled = false
		
		task.wait(0.4)
		
		light.Enabled = false
		
		mine.Transparency = 1
		
		sound.Ended:Wait()
		
		wait(5)
		
		mine.Transparency = 0.9
		
		DEBOUNCE = false
	end
end)

7 Likes

You just remove the part of the if statement that checks if the hit part’s parent has a humanoid.

Also remember to remove the hit.Parent.Humanoid.Health = 0 cause that will error if none humanoid objects touch the part!

6 Likes

3 ways,

  1. Check if a part touching the Object using :GetTouchingParts(), or :GetPartBoundsInBox()
    use OverlapParams to filter out other parts that would accidentally make the part explode (for example the floor, or itself when facing the right direction.

  2. check if the part was touched using a Touched Event (take from the post above)

  3. Raycasting

1 Like

Umm, Soooooo… how exactly do I do that? I’m a visual learner lol

Also I still want the character to die if the character touches the mine, If I remove the the hit.Parent.Humanoid the player wont die

1 Like
local mine = script.Parent
local minePosition = mine.CFrame.Position
local sound = script.Parent.BoomSound
local DEBOUNCE = false
mine.Transparency = 0.9

--[[
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
	humanoid.Health = 0
	sound:Play()
end
]]

mine.Touched:Connect(function(hit)
	if not DEBOUNCE then -- we removed the statement checking if hit.Parent had a humanoid
		DEBOUNCE = true
		
		if hit.Parent:FindFirstChild("Humanoid") then -- We moved the if statement here incase u still want players to instantly die when they touch the mine
-- I would instead kill the player if they are caught in the blast, instead of touching!
			hit.Parent.Humanoid.Health = 0
		end;
		sound:Play()
		local boom = Instance.new("Explosion")
		boom.ExplosionType = Enum.ExplosionType.NoCraters
		boom.BlastPressure = 300000
		boom.BlastRadius = 10
		boom.Parent = mine
		boom.Position = minePosition
		boom.Visible = true
		boom.Hit:Connect(function(part: BasePart, _)
			part.Anchored = (part == mine) or false
		end)
		
		local light = Instance.new("PointLight")
		light.Parent = mine
		light.Brightness = 10
		light.Range = 12
		light.Color = Color3.new(0.942306, 0.0528725, 0)
		light.Enabled = false
		
		task.wait(0.4)
		
		light.Enabled = false
		
		mine.Transparency = 1
		
		sound.Ended:Wait()
		
		wait(5)
		
		mine.Transparency = 0.9
		
		DEBOUNCE = false
	end
end)
2 Likes

How would I kill the player caught in the blast?

EDIT: I’m sorry for asking so many questions, I’m new.

1 Like

Replace your boom.Hit:Connection with the below:

	boom.Hit:Connect(function(part: BasePart, _) -- this fires for every part hit!
			part.Anchored = (part == mine) or false
			if part.Parent:FindFirstChild("Humanoid") then -- Check if one of the hit parts has a humanoid
		-- Now we kill the player cause he was caught in the blast!
				part.Parent.Humanoid.Health = 0
			end;
		end)

2 Likes

Okay, uh. New problems arose.

Okay, So basically when I Anchor the mine, for some reason it works when I step on the mine but when a part touches the mine it doesn’t explode. Also, when unanchored the mine explodes even when nothing is touching it. Although it might be cause of the terrain, I might need to whitelist the terrain.

1 Like

Could you send your current script and maybe give a visual (gif or video) of the other part that’s touching the mine? Make sure you have output open → go to view at the top → on the left side hover over the small icons till you see “output”. Send any errors

2 Likes

Here is when the part is unanchored:


https://gyazo.com/c67d0368bddcd6e3d0b9b81bc064bc5b
.
.
.

and here is when its anchored
https://gyazo.com/f6a5587fa72126b4b973e37c810ffdf8
Screenshot 2023-07-05 181637

here is the code:

Ooooo, I found something. Check this out.
https://gyazo.com/8a69d0f29b3243d0d15d66be4493f2b9

1 Like

When you’re testing in studio. You’re actually creating a part on the client. This mean’s only you see that brick that you added. The server doesn’t see the part you added. When testing a server script always test via server side. So test in studio → go to test at the top → switch to from client to server → then add the part and try again.

Also for this:

You saved the mines original position at the top of the script “local minePosition = mine.CFrame.Position”. So if the mine moved when the script ran then its setting the explosion to the old position. Simply just add it within the touched connection.

local mine = script.Parent
-- Removed the old mine position
local sound = script.Parent.BoomSound
local DEBOUNCE = false
mine.Transparency = 0.9

--[[
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
	humanoid.Health = 0
	sound:Play()
end
]]

mine.Touched:Connect(function(hit)
     local minePosition = mine.CFrame.Position -- Added it here instead so it gets the recent position
-- add the rest of your script here

end)

It still explodes even when its not moving

I need to whitelist the mine so if it detects anything else but the mine then it explodes, but I dont know how to do what

I think its detecting itself.

I want you to do 2 things:

  1. First verify that you don’t already have an explosion object inside the mine itself. Send a picture of the part in workspace with anything that’s inside it.

  2. If it isn’t the above. I want you to do a print right after the touch connection to see what is triggering it.

E.g

mine.Touched:Connect(function(hit)
  print(hit) -- we wanna see whats causing it to explode when server starts
-- Rest of your code as usual

Make sure to screenshot the output

1 Like

the terrain is hitting the part, the terrain is triggering the explosion…

yeah there isn’t already an explosion in the object.
Screenshot 2023-07-05 190428

Oh, didn’t even think that terrain would trigger it lol. You should just be able to do the following:

mine.Touched:Connect(function(hit)
	if hit:IsA("Terrain") then -- check if its terrain
		return; -- stops the function
	end;
-- rest of the code u had before here (with recent edits mentioned above)
end);
1 Like

what does return do?
I always see it but never know what it is

It returns a value from the function. If you have a function GetAverage() and you enter in an array of numbers, in the function you could code an average calculation and return the result. To use this function, you could write ‘’local avg = GetAverage({5, 15})’’. The variable avg would contain the average.

1 Like

Like the name says. It returns something within a scope. So for example:

Lets say I have a math function that adds two numbers:

function Add(Number1, Number2)
  local NewValue = Number1 + Number2;
end;

Instead of having to create a new variable like this:

local MyNewValue -- have to set a variable to store the result

function Add(Number1, Number2)
 MyNewValue = Number1 + Number2; -- updates our above variable
end;

Add(1, 4); -- Adding 1 + 4
print(MyNewValue) -- Will print 5

I can simply just return the result:

function Add(Number1, Number2)
   return Number1 + Number2; -- when this function is called it will go line by line and stop at this and return 1 + 4 (which is 5)
end;

print(Add(1, 4)) -- Will print 5 cause the function Add() returned the result

In your case when I do “return” with nothing after it. I am simply returning literally nil/nothing. The function will then stop at said return.

Hopefully, that made sense.

1 Like

Dude thank you very much for the help, you have no ideaaaaaaa

oooooo, Nice.

now I finally understand. Thank you