I am creating a door where players can unlock with a key. The keylock is destroyed once the player interacts with the door, and the door slams shut after 5 seconds. After slamming the door, the keyhole will reappear by cloning and the player will have to open the door with the key again.
However, when the keyhole is cloned after being destroyed initially, the decal (the lock) isn’t being cloned alongside it.
apologies if the format here is insufficient or unclear, it’s my first time making a forum post
here’s the code:
local function createKeyholes()
local keyholes = {}
if keyholeFront then
local front = keyholeFront:Clone()
front.CFrame = keyholeFront.CFrame
front.CanTouch = true
front.CanCollide = false
front.Transparency = 0
front.Parent = doorModel
table.insert(keyholes, front)
print("Created front keyhole")
end
if keyholeBack then
local back = keyholeBack:Clone()
back.CFrame = keyholeBack.CFrame
back.CanTouch = true
back.CanCollide = false
back.Transparency = 0
back.Parent = doorModel
table.insert(keyholes, back)
print("Created back keyhole")
end
return keyholes
end
local function onKeyTouched(hit, keyhole)
if hit.Name == "Key" and hit.BrickColor == keyhole.BrickColor then
-- Prevent multiple triggers
if hit:GetAttribute("Used") then return end
hit:SetAttribute("Used", true)
print("Playing lock sound")
lockSound:Play()
-- Make key non-interactive temporarily
local originalCanCollide = hit.CanCollide
local originalTransparency = hit.Transparency
hit.CanCollide = false
hit.Transparency = 0.7 -- Make key semi-transparent to show it's used
-- Remove keyholes
for _, kh in ipairs(doorModel:GetChildren()) do
if kh.Name:find("Keyhole") then
kh:Destroy()
end
end
setDoorState(true)
task.delay(AUTO_CLOSE_DELAY, function()
setDoorState(false)
task.delay(KEYHOLE_RESPAWN_DELAY, function()
local newKeyholes = createKeyholes()
for _, kh in ipairs(newKeyholes) do
kh.Touched:Connect(function(h)
onKeyTouched(h, kh)
end)
end
-- Reset key properties after door closes and keyholes respawn
hit.CanCollide = originalCanCollide
hit.Transparency = originalTransparency
hit:SetAttribute("Used", false)
end)
end)
end
end