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

Script to detect if parent touches brick?

So I managed to write a script that will unanchor the part when touched but how could I make it so that if it touches "Brick", it unanchors "Brick"?

function touch()
	script.Parent.Anchored = false
	
end

script.Parent.Touched:connect(touch)

2 answers

✓ Accepted answer
function touch(part)
	if part:IsA("Part") then --Checks if it's a Part
		script.Parent.Anchored = false
	end
end

script.Parent.Touched:Connect(touch)

or:

function touch(part)
	if part.Name == "Brick" then --Checks if it's a part named Brick
		script.Parent.Anchored = false
	end
end

script.Parent.Touched:Connect(touch)

So the parameter part is referencing on what touched the script's parent. Hope this helps!

3 comments

0

How would I make "Brick" unanchored though?

0

Never mind! I just edited a line and now it works! Thanks a bunch!

0

Just make sure you're learning.

Well, an easier way is to used Part.Touched:Connect(function() because it's just simple and understandable.

First, let's define all the variables;

local Brick = script.Parent

Simple right? This will guarantee that Brick is script.Parent.

Now we add the event that will fire when requirements are there. so it would look like;

local Brick = script.Parent

Brick.Touched:Connect(function()

end)

and lastly, we can do what we want to do. In this case, unanchor the brick;

local Brick = script.Parent

Brick.Touched:Connect(function()
	Brick.Anchored = false
end)

I'm not sure why your script isn't working but make sure this script is a **ServerScript** NOT a **LocalScript**.

If this helped then please accept answer ;)

Good luck developing!

2 comments

0

How would I be able to specify Touched because I want it so if a part touches it, it unanchors but if a humanoid, it wont unanchor.

0

Then get the player's body parts, so if Hit.Parent:WaitForChild('Character').WaitForChild('Left Arm') then return end

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