Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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)
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) Official_Raiku 0 — 6y
0
Is the door anchored? User#21908 42 — 6y
0
Yes the door is anchored I'll see if I cant post more info. Official_Raiku 0 — 6y
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. Zafirua 1348 — 6y
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. xJoshRyan 23 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

Answer this question