How to make door open for one player? (Client Sided)

Hello, I am making a simple keycard door and I would like it to only open for the person that opens it. I tried doing this by putting a local script inside of StarterPlayerScripts, here is the code:

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

It works with this but it opens for all players.

Does anyone know how to help? Thank you for reading!

4 Likes

Because all clients are checking if the door has touched a “Visa”

You need to specify the Player that touches it.

2 Likes

How could I do that? Thanks for helping.

1 Like
local Player = game.Players.Localplayer
local function onTouch(hit)
	if hit.Parent.Name == "Visa" and hit.Parent.Parent.Name = Player.Name then
		game.Workspace.BorderBlock.CanCollide = false
		wait(2)
		game.Workspace.BorderBlock.CanCollide = true
	end
end
game.Workspace.BorderBlock.Touched:Connect(onTouch)

Take note i did this fast and untested.
And im guessing Hit.Parent is a tool.

7 Likes

You can use RemoteEvent:FireClient()

4 Likes

Yes hit.Parent is a tool, I will try this right now.

1 Like

I don’t think he’s looking to check it serverside.
Even tho “CanCollide” is not really requested to be checked serverside as it can be edited clientside.

1 Like

Thank you so much, It worked! :slightly_smiling_face:

1 Like

Take note that script can be improved.

1 Like

How so? (Filler text for limit)

A better way to detect the player/tool.
Mostly currently the script will check if the player Character follows the player name.

Take note that Character name and player name are not always the same!

1 Like

Oh I see. Thank you! :smiley: It means a lot.

1 Like