All players can walk through a door if one person uses a keycard

Hello! I am trying to make a door where you need a keycard to enter. When the keycard touches the door it makes the can collide of the door to be false then true again after a couple of seconds. This works however players without a keycard are able to walk through the door if someone else uses a keycard. Is it possible to make it so only players with a key card can walk through the door?

1 Like

Trying to do the script with a local script or with RemoteEvent

1 Like

make it local so that they can’t see it open or go through.

1 Like

This is my script inside the part. How would I go about doing that as I tried to put it in a local script but then the door didnt work?

""local debounce = true

script.Parent.Touched:Connect(function(hit)
if hit.Parent.Name == “Ticket” then
if debounce == true then
debounce = false

		script.Parent.Transparency = 0.5
		script.Parent.CanCollide = false
		wait(2)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true

		wait(0.5)

		debounce = true
	end
else
	print("The keycard didn't touch it.")

end

end)“”

you’ll have to change some things in order for it to work.

1 Like

You can create the Part inside StarterPack and create a LocalScript with the same code I believe, just that you’ll need to change where the Touched Door part lies

2 Likes

I have done this but now the door does not appear in game :thinking:

Did you do something like this?

local debounce = true
local Door = game.ReplicatedStorage.Door:Clone()
Door.Parent = workspace

Door.Touched:Connect(function(hit)
    if hit.Parent.Name == “Ticket” then
        if debounce == true then
            debounce = false

		    script.Parent.Transparency = 0.5
		    script.Parent.CanCollide = false
		    wait(2)
	        script.Parent.Transparency = 0
		    script.Parent.CanCollide = true

		    wait(0.5)

		    debounce = true
        end
	end
else
	print("The keycard didn't touch it.")
end
-- local debounce = true

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Ticket" then
		if debounce == true then
			debounce = false

			script.Parent.Transparency = 0.5
			script.Parent.CanCollide = false
			wait(2)
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true

			wait(0.5)

			debounce = true
		end
	else
		print("The keycard didn't touch it.")

	end
end)
1 Like

You’d need to actually create/clone the Door inside ReplicatedStorage first, then Parent it to the workspace so that it knows what it’s looking for (Refer to my code)

1 Like

Im not quite sure what you mean. I now have the door part in replicated storage with a local script with your script in?

1 Like

Sorry for being a bit complicated Im still quite new to scripting and all of this :slight_smile:

Wait I think I see the issue, you’re getting the hit.Parent (And not the Hit Part’s Name), also the LocalScript should be inside the StarterPack

1 Like

When you detect a ticket, check if the ticket’s parent is a player’s character using GetPlayerFromCharacter
if it is a player use FireClient and tell the player’s client to open the door

So I need to do

if hit.game.Workspace.Part == “Ticket” then

rather than

if hit.Parent.Name == “Ticket” then

You could literally just do hit.Name, so let me clarify:

  • hit.Name refers to the actual Part that it touched (Whether it be a Left Arm, Right Leg, etc)
  • hit.Parent.Name refers to the Part’s Parent (Or the Player’s Character, in this instance “MarvelousMatty”
1 Like

Sorry Im still quite new to scripting and studio so I don’t really know how to use FireClients.

I linked the wiki page, but if you didn’t understand it, use this LocalScript, and change the remote’s name

game.ReplicatedStorage.RemoteEvent.OnClientEvent(function(door)

door.CanCollide = false
door.Transparency = .5
wait(2)
door.CanCollide = true
door.Transparency = 0
end)

and to fire it to the client, simply replace the door opening with

local plr = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
local door = script.Parent --if the script is in the door
if plr then
game.ReplicatedStorage.RemoteEvent:FireClient(plr,door)
end

Ok. I have tried that but now I get an error in the output and the part doesn’t appear in the workspace?

Output error
Roblox dev forum error

The local script

Explorer
Output dev forum

Ah yes, end statements and positionings
That’s just a common mistake people make, since you didn’t exactly end the function properly it resulted in that error

This should fix it though:

local debounce = true
local Door = game.ReplicatedStorage.Door:Clone()
Door.Parent = workspace

Door.Touched:Connect(function(hit)
    if hit.Parent.Name == “Ticket” then
        if debounce == true then
            debounce = false

		    Door.Transparency = 0.5
		    Door.CanCollide = false
		    wait(2)
	        Door.Transparency = 0
		    Door.CanCollide = true

		    wait(0.5)

		    debounce = true
        end
    else
        print("The keycard didn't touch it.")
	end
end)
2 Likes