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

How do I make a script run if it's touching a specific object?

Hello. I'm trying to make a script that runs one thing if it's touching an object, and run another script if it isn't. Say A is the object I want to touch object B. I'm trying to make it so that when a button is clicked, if A is touching B, then run script 1. If A isn't touching B, run script 2. The current script i'm using is

  script.Parent.ClickDetector.mouseClick:connect(function()
	if Workspace.Part.Touched:connect(function() then
			script.Parent.Parent.A.AlignOrientation.Enabled = true
			script.Parent.Parent.A.AlignPosition.Enabled = true
			wait(2)
			script.Parent.Parent.A.AlignOrientation.Enabled = false
			script.Parent.Parent.A.AlignPosition.Enabled = false

	end)
	else
			script.Parent.Parent.A.AlignOrientation.Enabled = false
			script.Parent.Parent.A.AlignPosition.Enabled = false
			wait(2)
			script.Parent.Parent.A.AlignOrientation.Enabled = true
			script.Parent.Parent.A.AlignPosition.Enabled = true
	
end)

It doesn't do anything when clicked, so any help fixing this would be appreciated. Thanks!

1 answer

✓ Accepted answer

Try making a touch function in the object and make it check whether the other one is touching if it is, it will run one script else, it will run the other.

EDIT:

ClickDetector Script:

script.Parent.ClickDetector.MouseButton1Click:Connect(function()
	--enable the script in object A or B
end)

In Object A or B:

script.Parent.Touched:Connect(function(hit)
	if hit.Name == B then
		--enable said script here
else
		--enable other said script

end
end)

You can do the same script in B, but change == B to == A. Also, disable these scripts, so that they only run once the button is clicked.

2 comments

0

I'm still a bit new at scripting, so how would you do that? Do you have any videos or forum links that can teach it?

0

I'll give you an example.

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