Keycard Door not Working?

Hello! This is a relatively simple topic but I seem to have an error within my keycard script where it should simply detect if the object hitting the door is the “Visa” which I have made and will set the door’s CanCollide to false.

The Code:

local function onTouch(hit)
	if hit.Parent.Name == "Visa" then
		script.Parent.CanCollide = false
	end
end
script.Parent.Touched:Connect(onTouch)

It is a local function as I only want the door to open for one player.

The keycard is just simply this:
image

Thank you for reading!

3 Likes

Try printing out the hit and hit.Parent variables :thinking: That will give you a headstart on what the issue could be

1 Like

I believe the door would detect the handle

hit prints out as the handle, and hit.parent just simply doesn’t print at all, strange.

1 Like

Wait If It’s local script It won’t run in workspace.

1 Like

It is a server script, just a local function.

1 Like

Wait, couldn’t you just make the Handle’s name to the Visa instead? Or do you want the Tool’s actual name?

1 Like

If I change the Handle’s name it wouldn’t be able to be held, I am going to make an invisible part inside of the tool that will be checked.

1 Like

Hm, and hit.Parent prints nothing which is odd (Are you sure it doesn’t print out as nil?)

1 Like

If your method doesn’t work, then try to find the tool in the players model

1 Like

No, it prints nothing at all. (Filler text)

1 Like

Where is the script / inside of what ? And btw “local function” doesn’t mean that It works for one player.

1 Like

It works now, it was my fault. In order to obtain the card you it was cloned from replicated storage from a local script, so the server script couldn’t detect it. I am sorry for wasting everyone’s time.

2 Likes

How would I make the door only open for one player then? Thank you for helping.

1 Like

I think you could actually do it from a local standpoint if that’s what you’re getting at?

--LocalScript inside StarterPack
local Door = game.ReplicatedStorage.Door:Clone()

I could be wrong though if Touched Events don’t exactly work locally-wise
(I have done a diddily dumb mistake oops)

1 Like

It should work…

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- verifys if its a player Note only players has humanoids
	if hit.Parent.Name == "Visa" then
		script.Parent.CanCollide = false
	end
end
end)

Using Local Script to do something with server side is not possible. Local Script can’t clone something what’s inside ServerStorage. I suggest moving Doors to ReplicatedStorage and cloning it from there

--LocalScript inside StarterPack/StarterGui/StarterPlayer
local Door = game.ReplicatedStorage.Door:Clone()

local function onTouch(hit)
	if hit.Parent.Name == "Visa" then
		Door.CanCollide = false
	end
end
Door.Touched:Connect(onTouch)
3 Likes

I’m confused, why is the script in starter pack? Isn’t that for tools? And am I detecting if the real door is getting touched or the cloned one?

1 Like

Just gonna reference this here:

2 Likes

If you use only one doors do this inside local script:

local function onTouch(hit)
	if hit.Parent.Name == "Visa" then
		game.Workspace.Door.CanCollide = false
	end
end
game.Workspace.Door.Touched:Connect(onTouch)

The replicated storage option is if you want to use many doors.

The script is in StarterPack because It’s client part of Services It could have been in StarterGui or any other client part of Services.

1 Like