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.
check if the part was touched using a Touched Event (take from the post above)
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)
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)
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.
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
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.
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)
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.
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
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);
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.
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.