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

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

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

✓ Accepted answer

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.

2 comments

0

Thanks! This was the solution!

0

You should add a debounce too though...

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)

1 comment

0

Still didn't work...

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