My proximityprompt script doesn’t work and I can’t find the error

Hello! I am new to scripting and I’ve recently made this with some help from the Roblox Documentation. For some reason, it doesn’t work and I can’t find the error.
The thing I want to achieve is that when you use the proximityprompt the door opens and when you use it again it closes
The issue is that when I use it doesn’t work.
Here’s a video:

I’ve tried asking other people for help but it didn’t work.
Here’s the script:

local ProximityPromptService = game:GetService("ProximityPromptService")
local door = game.Workspace.DoorLobby

closed = true

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

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

ProximityPromptService.PromptButtonHoldEnded:Connect(function()
  if closed == true then
     open()
     closed = false
end
  if closed == false then
     close()
     closed = true
  end
end)

I would really appreciate if you helped me. Thank you for reading.

Hi, is there 2 DoorLobby in your workspace? This should be the problem there. I also see that you are using the event PromptButtonHoldEnded and you are also using the service: ProximityPromptService for making script like this you need to make something like this:

local door = game.Workspace.DoorLobby
local prox = path.to.proximityPrompt
closed = true

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

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

prox.Triggered:Connect(function()
	closed = not closed
	if closed == true then
		close()
	else
		open()
		
	end
end)

You just need to change the variable prox to your proximity prompt instance!

Currently, you are always opening then immediately closing the door.
The problematic bit of code is below:

if closed == true then
     open()
     closed = false
end
if closed == false then
   close()
   closed = true
end

The second if statement always ends up firing, due to the value of closed being set to false in if it was true initially.

Consider using an else statement:

ProximityPromptService.PromptButtonHoldEnded:Connect(function()
  if closed == true then
     open()
     closed = false
   else
     close()
     closed = true
  end
end)
1 Like

I get an error there, what should I write?
Edit: It now works, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.