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

Help with sliding door script problems?

Asked by 9 years ago

I've got this sliding door script, and it is meant to move the Door down 15 studs, wait 3 seconds, then move it back up when a brick is touched. The only problem is that when I touch it, it's Y coordinates go all the way down to about -700,000. I've discovered that the problem is that it is registering that I am hitting the brick hundreds of times, rather than 1 like I want. I tried implementing my debounce to help this, but I still have the same problem.

What is my problem?



function onTouched(hit) debounce = script.Parent.Debounce.Value player = hit.Parent if hit.Parent ~= nil and debounce == false then debounce = true pos = 33.5 for i=0.1,150 do wait(0.01) script.Parent.Parent.Door.CFrame = script.Parent.Parent.Door.CFrame * CFrame.new(0, pos - i, 0) pos = pos - i end wait(3) pos1 = 18.5 for i=0.1,150 do wait(0.01) script.Parent.Parent.Door.CFrame = script.Parent.Parent.Door.CFrame * CFrame.new(0, pos1 + i, 0) pos1 = pos1 + i end debounce = false end end script.Parent.Touched:connect(onTouched)
0
I edited my answer to explain what I meant better. BlueTaslem 18071 — 9y

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Well idk why you're making the debounce so complicated, also your CFraming methods are kinda iffy. I'll just do this and let you examine over it.

db = false

script.Parent.Touched:connect(function(hit)
    if db == false then db = true end
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        for i = 0,15,.1
            script.Parent.Door.CFrame = CFrame.new(script.Parent.Door.Position + Vector3.new(0,-i,0))
            wait(.1)
        end
        wait(3)
        for i = 0,15,.1
            script.Parent.Door.CFrame = CFrame.new(script.Parent.Door.Position + Vector3.new(0,i,0))
            wait(.1)
        end
    end
    db = true
end)

I believe that would work. Give me feedback if not.

-Goulstem

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You are writing to debounce (line 5, line 20) but reading from script.Parent.Debounce.Value.

You do not here change script.Parent.Debounce.Value, so every time it does the debounce check, it continues on.

Just eliminate line 2, and the debounce will operate correctly (it would be cleaner to state above the function instead debounce = false)


In addition, your way of moving the door is problematic.

You are writing to the door CFrame the current door CFrame plus a changing number. This will make the door move much more than you planned.

Instead of using script.Parent.Parent.Door.CFrame in lines 9 and 17 {on the right of the equals sign only}, you should use a home variable, where home was set at the beginning of the script to script.Parent.Parent.Door.CFrame.


Something like this:

local home = script.Parent.Parent.Door.CFrame;
local pos = 18.5
 for i=0, pos, 0.1 do
    wait(0.01)
    script.Parent.Parent.Door.CFrame = home * CFrame.new(0, pos - i, 0)
end
0
What do you mean "home"? I don't understand, could you give me an example of the variable you are getting at here? (Please respond to this comment on my question, not this answer, so I see it in my notifications.) SlickPwner 534 — 9y
0
I tried that, and it moved a lot smoother, as it was actually slow enough to be visible moving, but it still registered me hitting it many times, therefore moved thousands of blocks. SlickPwner 534 — 9y
0
Have you also changed the appropriate debounce code that I proposed before the code snippet...? Have you tried debugging yourself with `print` statements? BlueTaslem 18071 — 9y

Answer this question