Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i make it so only parts with a specific name unanchor?

I have a hammer that makes parts unanchor when they get hit by it, but i only want it to break blocks that are named "Breakable". Here's my script:

local function onTouch(hit)
	hit.Anchored = false
end
script.Parent.Touched:Connect(onTouch)

3 comments

0

You have to specify what the part is going to get hit by like a human a hammer i think that is why also which type of script is it?

0

and you have to specify what part it is it or name

0

check if the Name of hit is "Breakable"?

2 answers

We know that we want to set the Anchored property to false whenever the part's name is Breakable. To achieve this, you can simply add an if statement inside your event, check if the name indeed is Breakable and if so, set the anchor to false. To check a name, the property's name is simple: Name.

if (part.Name == "Breakable") then
    part.Anchored = false
end

You add this to your code, in your case the part is called hit.

Also it is worth noting that if this is a local script, then this will be executed client-side and therefore only visible by the player that this script ran on. You'll likely want this to be in a script for it to be visible to everyone.

Resources:

if statements in Lua

Instance.Name

Script vs Localscript

it can be done by looping through the workspace or any service and get the baseparts and check if their name is unbreakable, I recommend making this a function so you can check any folder, and also to make a table for names that make the part anchored and then check for the parts

local names = {"Unbreakable"}
local function checkForParts(folder)
	if folder then
		for _, v in pairs(folder:GetDescendants()) do
			if v:IsA("BasePart") and table.find(names, v.Name) then
				v.Anchored = false
			end
		end
	end
end)

script.Parent.Touched:Connect(function(hit)
	checkForParts(hit.Parent)
end)

1 comment

0

He only wanted to check for "Breakable" blocks and unachor them. If you want this done recursively then cool.

This discussion is preserved as it was. New answers, comments, and votes are closed.