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

Why does this door lose the click detector if there is not key held?

I am trying to get the door to stay closed if you don't have a key and show a message that you need a key to unlock it. When the key is picked up you are then able to open it. The key works just fine on the door, but when you don't have a key and click the door, The click detector is destroyed and the sound plays which is what I don't want to happen, the door stays closed though, which is what I want it to do.

local door = script.Parent
local click = door.ClickDetector
local value = game.Workspace.Key.Value
local hidden = game.Workspace.door1

local function onClick(Clicked)
    if value.Value == true then
        door.Position = Vector3.new(33.9, 65.3, 7.6)
		script.Music:play()
		wait(0.33)
		script.Music:Stop()
		click:Destroy()
        wait(1)
        hidden:Destroy()
else
	if value.Value == false then
	local scrgui1
    if not Clicked.PlayerGui:FindFirstChild('ScreenGui') then
        scrgui1 = Instance.new("ScreenGui", Clicked.PlayerGui)
    else 
        scrgui1 = Clicked.PlayerGui.ScreenGui
    end
    local alert = Instance.new("TextLabel",scrgui1)
    alert.Text = "You need a key to unlock this."
    alert.Position = UDim2.new(0.5,0,0.7,0)
	alert.TextStrokeColor3 = Color3.new(0,0,0)
	alert.TextStrokeTransparency = 0
	alert.TextColor3 = Color3.new(255,255,255)
	alert.FontSize = Enum.FontSize.Size18
	script.Music:play()
	wait(0.4)
	script.Music:stop()
	click:Destroy()
	wait(2)
	scrgui1:Destroy()
	alert:Destroy()
		end
	end
end

click.MouseClick:connect(onClick)

1 comment

0

*"If you don't have a key" *That references to the character But the key is actually placed on workspace, So technicially the user is not holding it at all?

1 answer

✓ Accepted answer

In the "else" statement, you tell the script to delete the clickdetector and play the sound.

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