Making Keypad Door Client Sided

Recently, I made a door that opens when you correctly “type” in a 4 digit code (this uses clickable parts). Here’s a GIF of the door doing its thing: https://gyazo.com/8e5a33cc781c7145eec9e086b1cafb2d

However, I realized that if two or more people are trying to open the door at the same time they would mess up each others codes (if one person knows the code, another can mess them up and no one gets in).

How would I go about making this client sided?

I don’t know if code is necessary to solve this, but here’s the basics:

local key = {1,2,3,4}  --4 digit code for door
local code = {nil,nil,nil,nil} --holds values until full. checks agaisnt key
	
--player clicks key, goes to function (one for each button. efficency through the roof here)
keypad:FindFirstChild("1").ClickDetector.MouseClick:Connect(function(plr)
	addData(plr,1)
end)

function addData(player,data)
	--adds player click to code
	local index = nil
	for i = 1,4,1 do
		if code[i] == nil and index == nil then
			code[i] = data
		end
	end
		
	if index == 4 then
		--checks code agaisnt key
		local correct = true
		for i = 1, 4, 1 do
			if code[i] ~= key[i] then
				correct = false
			end
		end
		--opens door if correct
		if correct then
			openDoor()
		else
			code = {nil,nil,nil,nil} --resets code input if incorrect
		end
	end
end
2 Likes

Just put this functionality in a LocalScript in StarterCharacterScripts. That way, each player’s inputted code is stored in their own memory, so it can’t interfere with other players’ codes.
If you also put the openDoor() function in this LocalScript, the door will only open for the player meaning that only the player who put the correct code in could walk through the door.

1 Like