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

Door Script Ontouch Problems, Door is flying to other side of map? Ontouch not stopping?

I made sliding glass doors using CFframe+Vector3 line, its activated using a .touched:connect(Function) but the problem im having is when touching the part the script isnt following thru, the door is just FLYING to the other side of the map.

function DoorTouch(hit)
for I=1,20 do
	wait(.2)
		script.Parent.CFrame=script.Parent.CFrame+Vector3.new(.3,0,0)
end
	wait(4)
for I=1,20 do
	wait(.2)
		script.Parent.CFrame=script.Parent.CFrame+Vector3.new(-.3,0,0)
	end
end
game.Workspace.DoorSensor.Touched:connect(DoorTouch)

5 comments

0

I'm trying to get the door to open and close at the touch of the door sensor (Part is collide = false, invisible and almost CFramed into the ground)

0

Is the door anchored?

0

Yes the door is anchored I'll see if I cant post more info.

1

first of all, if you are using Touched function, always consider adding in a debounce since the Touch Event fires numerous times per second.

0

you should detect if the object that touched it is a humanoid if it's supposed to be touched by a player to open.

2 answers

A touched function will fire numerous times in a second, thus running the code numerous times a second. To prevent this we use a debounce. A debounce is just a variable that we change to true when the code runs and require it to be false for it to run again. Adding one would make your script look like this:

orTouch(hit)
if debounce == false then
     for I=1,20 do
          wait(.2)
        
 script.Parent.CFrame=script.Parent.CFrame+Vector3.new(.3,0,0)
end
    wait(4)
for I=1,20 do
    wait(.2)
        script.Parent.CFrame=script.Parent.CFrame+Vector3.new(-.3,0,0)
          end
 debounce = false
    end
end
game.Workspace.DoorSensor.Touched:connect(DoorTouch)

I apologize if this is messy as I am on a phone right now, if the output gives you any errors please leave a comment and I'll get back to you ASAP

-Cmgtotalyawesome

You should detect if it was touched by a humanoid if you want it so that it opens on player touch.

function DoorTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		for I=1,20 do
    			wait(.2)
        		script.Parent.CFrame=script.Parent.CFrame+Vector3.new(.3,0,0)
		end
    		wait(4)
		for I=1,20 do
    			wait(.2)
        		script.Parent.CFrame=script.Parent.CFrame+Vector3.new(-.3,0,0)
    		end
	end
end

game.Workspace.DoorSensor.Touched:connect(DoorTouch)

sorry for formatting, on mobile.

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