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

Door lock script is toggling CanCollide on a loop when I don't want it to?

Asked by 6 years ago

Having a strange problem here, I've created a script to open a door with a key.

function onTouched(hit)
    if hit.Parent.Name == "Room1"
        then script.Parent.CanCollide = false
        wait(1)
        script.Parent.CanCollide = true
    end
end


script.Parent.Touched:connect(onTouched)

This works just fine, but I also want it to be opened by a master key. When I try to add that to the script, the door constantly goes collidable and noncollidable on a loop, even when I'm not holding anything.

function onTouched(hit)
    if hit.Parent.Name == "Room1" or "MasterKey"
        then script.Parent.CanCollide = false
        wait(1)
        script.Parent.CanCollide = true
    end
end


script.Parent.Touched:connect(onTouched)

Please help?

2 answers

Log in to vote
1
Answered by
Nosteal 50
6 years ago

The problem is in line 2, where you put in

if hit.Parent.Name == "Room1" or "MasterKey"

The problem is, you didn't specify the condition for "or", so the game checks whether the key's name is Room1, and then it checks if the string "MasterKey" exists, which it does by default.

You instead need to do

if hit.Parent.Name == "Room1" or hit.Parent.Name == "MasterKey"

This checks whether the key's name is Room1, or whether it's name is MasterKey.

0
Thanks! This was the solution! MrMauio 2 — 6y
0
You should add a debounce too though... Pojoto 329 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Try this:

function onTouched(hit)
    local open = false
    if hit.Parent.Name == "Room1" or "MasterKey" then
        if open == false then
            open = true
            script.Parent.CanCollide = false
            wait(1)
            open = false
            script.Parent.CanCollide = true
        end
    end
end

script.Parent.Touched:connect(onTouched)

0
Still didn't work... MrMauio 2 — 6y

Answer this question