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

Sliding Door is not changing position when clicked? [Solved]

I've attempted to make a sliding door that changes position when clicked. I've soon to add the animation, as I wanted to make sure the door changed position. When I click the door, I receive nothing from the output. Any help would be appreciated, and any answers that work or help will be upvoted. Thank you.

--[[
	DoorClosedPosition = 8.5, 5, -0.5
	
	DoorOpenPosition = 13.5, 5, -0.5
--]]

partPos = script.Parent.Position
part = script.Parent

if partPos == Vector3(8.5, 5, -0.5) then
	part.ClickDetector:MouseClick(function()
		partPos = Vector3.new(13.5, 5, -0.5)
	end)
else 
	part.ClickDetector:MouseClick(function()
		partPos = Vector3.new(8.5, 5, -0.5)
	end)

end

2 answers

This mostly requires some reorganizing and small fixes.

On lines 11 and 15 you didn't properly connect the event. On line 10, using the if statement outside of the event will only allow the door to be closed and not opened, or opened and not closed. On line 12 and 16 you try to move the block by setting a variable, instead you needed to set the block's property.

local part = script.Parent
local open = false
part:WaitForChild("ClickDetector").MouseClick:connect(function()
     if open then
          open = false
          part.Position = Vector3.new(8.5,5,-0.5)
     else
          open = true
          part.Position = Vector3.new(13.5,5,-0.5)
     end
end)

You're doing this in what I think may be a bit more difficult than what is needed. First of all, I don't suggest checking the CFrame to see whether it is open or closed, just use logic. Use a Variable that will change when clicked, more information and a functional script bellow.

local Status = "Closed" -- Starting with the door closed, make sure it's at it's Closed position!
part = script.Parent

part.ClickDetector.MouseClick:connect(function() -- Set up the click event
	if Status == "Closed" then -- Check whether its Open or Closed
		part.Position = Vector3.new(13.5, 5, -0.5) -- It's closed, they clicked, so we want it open
		Status = "Open" -- It's open now!
	elseif Status == "Open" then
		part.Position = Vector3.new(8.5, 5, -0.5)
		Status = "Closed"
	end
end)

If you have any questions, just comment them Bellow!

If this helps, accept the answer

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