KeyCard Door Help

Hello.
I am having a problem with my keycard door.
There is no error.
Code:

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name == 'KeyCard' then
		script.Parent.CanCollide = false
	else
		script.Parent.CanCollide = true
	end
end)

And I touched the door, and it does not work.

2 Likes

Are you sure you hit the door with a tool named KeyCard?

What’s the part name inside the tool. I assume you have the tool named keycard and not the actual part that’s touching it.

1 Like

It has a part named Handle inside of the tool

Your error is here:

if Hit.Parent.Name == ‘KeyCard’ then

You should be referencing the KeyCard’s name instead of the parent, simply change it to:

"If Hit.Name == ‘KeyCard’ then …(rest of your code)

local debounce = false

script.Parent.Touched:Connect(function(Hit)
	if debounce then return end
	debounce = not debounce
	if Hit.Parent.Name == 'KeyCard' then
		script.Parent.CanCollide = false
	else
		script.Parent.CanCollide = true
	end
	wait(.5)
	debounce = not debounce
end)
1 Like

Nvm I apologize I read your code wrong. You were doing hit.parent nvm ignore me

1 Like

Hit.Parent is referring to the tool itself. The hit is the handle part.

Then unless there was something misspelled, the script should work correctly.

Without a debounce, it would just repeatedly change the part’s CanCollide to true/false whenever .Touched is fired…

2 Likes

If hit.Parent.Name ~= “KeyCard” then maybe try adding this

if(hit.Name == "KeyCard" or hit.Parent.Name == "KeyCard") then

ANd as stated above, a debounce would also be the way to go here,

I agree with Avvalor. 30 CHARRRSSSS

1 Like

Tried that and it does not work.

Have you tried this? It should work.

3 Likes

It does not work.
[30characters]

I tried it myself in studio and it does work. Make sure to create a tool, name it “KeyCard” and put a part named “Handle” inside of it.

1 Like

Never mind it works.
[30chars]

Make sure to mark the solution.