Help making breakable blocks

Hello there this is my first time using this category
Anyhow like the title says I want to make a breakable block that only breaks when a key is pressed and the characters Are on top of it. Please see the video below on what I want to achieve

Thank you

Note: I’m not asking you to spoon feed me code any tut or explanation would be nice

1 Like
  1. Variable Bool.
  2. Detects that the player presses the key.
  3. The animation is activated and the variable will change to true.
  4. A touch event that detects what the player is stepping on.
  5. If the block is something that can be destroyed, it will be destroyed.
  6. When the player touches the ground, the variable will change to false.

Go through each step and you will have your script.
PS: requires RemoteEvent.

I’m a little confused
could you try explaining the touched event a bit more?
Like how the script would check to do the touched event

BasePart.Touched is used to detect when something is touching another brick

BasePart.Touched:Connect(function(partThatTouched)
  -- do stuff
end)

It fires multiple times at once however, so you’d need to add a debounce system to it

No, I mean in the sense that how would I tell the script to run the script when the bool value
is made?

This will tell you what object the player is touching.

local Player = game:GetService("Players").LocalPlayer
Character = Player.Character or Player.CharacterAdded:Wait()

local Bool = false

for i,Part in pairs(Character:GetDescendants()) do
    Part.Touched:Connect(function(hit)
	    if not hit:IsDescendantOf(Character) then
            print(hit)
	    	if Part.Name == "Destroy" then
	    	    Bool = true
	    	elseif Part.Name == "Floor" then
	    	    Bool = false
            end
    	end
	end)
end

Still confused about how to keep the event from running until the bool value is met

I’m a little confused with your question

local blockWasTouched = false -- bool value

BasePart.Touched:Connect(function(part)
   if not blockWasTouched then
       blockWasTouched = true

     -- the script that runs the runs what's suppose to happen
   end
end)

Unless you mean making the script detect when a bool value in created in the explorer

Instance.ChildAdded:Connect(function(child)
   if child:IsA("BoolValue") then
      -- do stuff
   end
end)

You could use events for that

Event:Wait() -- this stops the script until the event is fired
1 Like

would that mean I have to use if statements?

Yes, if you want to check if whatever was added was a BoolValue

local function WaitForChild(parent, child) : Instance -- function to wait for a child to be added
   local c = parent.ChildAdded:Wait() -- wait for the child to be added

   if c:IsA(child) then -- check if it's the specified type
      return c -- return it
   else
      return WaitForChild(parent, child) -- repeat
   end
end

local boolValue = WaitForChild(Instance, "BoolValue")

Are you asking for lets say if I press E on my keyboard, breakable blocks will break around me?

yes basically but only the block your touching

You could use a region3 to detect parts around the player and use UserInputService to detect key presses.
Here are some tutorials that helped me:
UserInputService: Key Press Detection (Press E) - UserInputService - Advanced Roblox Scripting #2 - YouTube
Region3: Advanced Roblox Scripting Tutorial #17 - Region3 (Beginner to Pro 2019) - YouTube

wow sounds good ill tell you if it works when i get some time this weekend.

Depending on how you want to handle it, you could have a true/false variable that toggles when the blocks are near you. Then if it’s on (when the blocks are detected), you can allow the player to press a key to break them.

Or blocks can constantly be checked around you, and if they detect the breakable blocks (via certain names, part has something inside of it, etc.), you could be able to break them.

In the case you want to do the latter, RunService could be a benefit for that.

Hmm sounds good in this case do I use regoin3 or another function?

Yes, like what Tedted659 said.

thxs for your contribution! In update this post if i make any breakthroughs!

1 Like

I would put an object you would barely or never use inside of the breakable object so the script can detect it from FindFirstChild, for example I would put a beam inside of it so a script can find every block that has the beam inside and destroy it if its in the region3. Probably not efficient but it works for me.

No problem!

Also, are you intending on making this thing server-sided?