Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I tried to Make a Keycard Door What am i doing wrong?

i Probably did do everything wrong cuz this is my first try with custom functions and i never EVER Made a keycard door

door = script.Parent

function Touched()
	DoorOpen()
	DoorClose()
	
	
end

game.StarterPack.Keycard:connect(Touched())
local function DoorOpen()
	script.Parent.Transparency = 0.50
	script.Parent.CanCollide = false
	
	
local function DoorClose()
	script.Parent.Transparency = 0
	script.Parent.CanCollide = true
	
	end
end

1 answer

Improvements

You made a local variable for the door but didnt use it

Connect should be used instead of connect [Deprecation]

Issues

Local Functions should be defined before they are called, not after

You did not bind the KeyCard connection with an event

The player's backpack contains the tool the player uses not the StarterPack

Information

Below I've used a RemoteEvent to allow the player's activation of the tool to affect the server

Added a debounce to prevent spam

Additional Local Script (under the Tool "KeyCard")

local keycard = script.Parent
local remote = game:GetService("ReplicatedStorage"):WaitForChild("KeyCardEvent")

keycard.Activated:Connect(function()
	remote:FireServer()
end)

Revised Server Script (under the Part "Door")

local door = script.Parent
local debounce = false

local remote = Instance.new("RemoteEvent")
remote.Name = "KeyCardEvent"
remote.Parent = game:GetService("ReplicatedStorage")

local function DoorOpen()
	door.Transparency = 0.5
	door.CanCollide = false
end

local function DoorClose()
	door.Transparency = 0
	door.CanCollide = true
end

remote.OnServerEvent:Connect(function(player)
	if debounce == false then
		debounce = true
		DoorOpen()
		wait(1.5)
		DoorClose()
		wait(1.5)
		debounce = false
	end
end)

1 comment

0

hmm, good for one door.. i think I'd use ray to see what door is close by and the player is facing and then if the distance is short enough, allow the keycard to work - maybe if its close to the keycard/ fab reader.

This discussion is preserved as it was. New answers, comments, and votes are closed.