Detect Workspace Explosions?

Hello there, I am trying to make a players camera shake whenever they are nearby an explosion in the workspace. Is there a way to make a script that monitors, if there are any explosions in the workspace so then I can add a camerashake. any help is appreciated!

1 Like

You can connect a function to a ChildAdded event on the workspace, which would call the function when a new instance is added to the workspace, which in this case would be the explosion, or the part that the explosion is in. For example:

workspace.ChildAdded:Connect(function(Child)
    if Child.Name == "Explosion" then
        print("Explosion Found")
    end
end)

Documentation here: Instance | Roblox Creator Documentation

3 Likes

Put this in a local script

local plr = game.Players.LocalPlayer
local pchar = game.Workspace:WaitForChild(plr.Name)

local MinDistance = 50 --Put distance here

function GetDistance(part1: (Instance|Vector3), part2: (Instance|Vector3))
	local epos = part1.Position
	local hpos = part2.Position
	local distV3 = Vector3.new(hpos.X - epos.X, hpos.Y - epos.Y, hpos.Z - epos.Z)
	local dist = math.abs((distV3.X + distV3.Y + distV3.Z) / 3)
	return dist
end

game.Workspace.DescendantAdded:Connect(function(child)
	if child:IsA("Explosion") then
		local distance = GetDistance(pchar.Head, child)
		print(distance)
		if distance < MinDistance then
			print("Close Explosion!")
			--Code Here
		end
	end
end)

It gets the distance between two objects / coordinates and will react accordingly! Feel free to remove the prints they are just for testing :grinning_face_with_smiling_eyes:

This will work in a server script aswell, just change the objects / positions

Edit: I changed ChildAdded to DescendantAdded so it checks in all parts of the workspace when added!

1 Like

You can simply use magnitude and the camShake module.

You will be cloning the explosion VFX to the workspace in the first place, so you dont need to check if there is an explosion in the workspace

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = player.Character:WaitForChild("HumanoidRootPart")

local explosion = --cloned explosion VFX

local max_camShake_Distance = 10 --put max shaking distance here

local Magnitude = (hrp.Position - explosion.Position).Magnitude

if Magnitude < max_camShake_Distance then
	--do your camera shake magic here
end
1 Like

Thank you very much the code worked, if you dont mind, can you explain what this part of the code does:

        local distV3 = Vector3.new(hpos.X - epos.X, hpos.Y - epos.Y, hpos.Z - epos.Z)
	local dist = math.abs((distV3.X + distV3.Y + distV3.Z) / 3)
1 Like

It subracts the first vector by the second, then converts it to a positive number so that the script can read the distance!

1 Like