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

How to make an object work only if touching another object?

Hello. I'm trying to make a script where it only works if it's touching another specific object. The current script I use is

 local function onTouch(hit)
	if (hit.Parent.Parent.Parent) then
		print(hit)
	script.Parent.Parent.Object1.AlignPosition.Enabled = true
	wait(1)
	script.Parent.Parent.Object1.AlignPosition.Enabled = false
	end
end

script.Parent.Touched:connect(onTouch)

I've tried doing

 local function onTouch(hit)
	if (hit.parent.findFirstChild("Object2")) then
		print(hit)
	script.Parent.Parent.Object1.AlignPosition.Enabled = true
	wait(1)
	script.Parent.Parent.Object1.AlignPosition.Enabled = false
	end
end

script.Parent.Touched:connect(onTouch)

but it doesn't work. When I use the first script, anything touching Object1 activates AlignPosition, but I only want it that when it touches Object2 it activates AlignPosition. Any help is appreciated. Thanks!

1 answer

✓ Accepted answer
 local function onTouch(hit)
    if hit.Name = "Object2" then
        print(hit)
    script.Parent.Parent.Object1.AlignPosition.Enabled = true
    wait(1)
    script.Parent.Parent.Object1.AlignPosition.Enabled = false
    end
end

script.Parent.Touched:connect(onTouch)

That should work

3 comments

0

Yep, it works. Thank you so much!

0

np

0

btw, your way could also work but you used "." and not this ":"

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