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

SCP script door part2 How my door turn?

Why does the door turn? How to make so that she not turn?

deboulse = false

script.Parent.Button.ClickDetector.MouseClick:connect(function(player)
	if deboulse == false then
		deboulse = 2
		print(player.Name.." open door")
		for i = 0,005 do
			script.Parent.Parts.Union1.CFrame = script.Parent.Parts.Union1.CFrame*CFrame.new(0,0,3)
			script.Parent.Parts.Union2.CFrame = script.Parent.Parts.Union1.CFrame*CFrame.new(0,0,3)
			script.Parent.Parts.Part1.CFrame = script.Parent.Parts.Union1.CFrame*CFrame.new(0,0,3)
			script.Parent.Parts.Part2.CFrame = script.Parent.Parts.Union1.CFrame*CFrame.new(0,0,3)
			wait()
		end
		for index, light in pairs(script.Parent.lights:GetChildren()) do
			light.Color = Color3.new(1,0,0)
		end
		script.Parent.lights.BrickColor = BrickColor.new("Bright red")
		deboulse = true
	elseif deboulse == true then
		deboulse = 2
		print(player.Name.." close door")
		for i = 0,005 do
			script.Parent.Parts.Union1.CFrame = script.Parent.Parts.Union1.CFrame+Vector3.new(0,0,3)
			script.Parent.Parts.Union2.CFrame = script.Parent.Parts.Union1.CFrame+Vector3.new(0,0,3)
			script.Parent.Parts.Part1.CFrame = script.Parent.Parts.Union1.CFrame+Vector3.new(0,0,3)
			script.Parent.Parts.Part2.CFrame = script.Parent.Parts.Union1.CFrame+Vector3.new(0,0,3)
			wait()
		end
		for index, light in pairs(script.Parent.lights:GetChildren()) do
			light.Color = Color3.new(1,1,1)
		end
		script.Parent.lights.BrickColor = BrickColor.new("Institutional white")
		deboulse = false
	end
end)

Explorer

1 comment

0

Have you tried using a HingeConstraint to make your door close? http://wiki.roblox.com/index.php?title=API:Class/HingeConstraint

1 answer

Hey NiniBlackJackQc,

By translating the door's position using a Vector3, you are 'resetting' the door's rotation. I recommend a different approach to translating the door:

  • Take all the parts in the door, and put them in a model inside the existing door model. Name the model 'SlidingDoor.'

  • Create a part and have it roughly the same size and position as the door. Have the front of the part face the direction you wish the door to move. This is important, as if you fail to do so, the door will not open in the right direction.

  • Put the part in the door model you made in the first step. Make the part invisible and shut off collisions; this part does not need to be part of gameplay, it is merely for translation purposes. Optionally, name it something along the lines of 'PrimaryPart' so that it will be easy to identify.

  • Select the model you have just created, and select its 'PrimaryPart' property. This will change your cursor: select the part you have just created.

We now have a model containing each component of the door, plus a primary part. The purpose of the primary part is to serve as a relative position for the model; if you move the primary part, each part in the model will move with it. Applying this, let's go to our code:

local Parent		= script.Parent;
local Door		= Parent:WaitForChild("SlidingDoor");
local Button		= Parent:WaitForChild("Button");
local Lights		= Parent:WaitForChild("lights"):GetChildren();

local IsOpen	= false;
local Ready	= false;

-- Don't touch anything but this:
local Speed	= .1;

local function ToggleLights()
	local newColor = IsOpen and Color3.new(1, 1, 1) or Color3.new(1, 0, 0);

	for k, light in pairs(Lights) do
		light.Color = newColor;
	end;
end;

local function ToggleDoor()
	if (Ready) then
		Ready = false;

		-- The direction PrimaryPart is facing, multiplied by speed and then multiplied again by 1 or -1 based on the current door position.
		local moveDirection = Door.PrimaryPart.CFrame.lookVector
							* Speed
							* (IsOpen and -1 or 1);

		-- Move amount is the size of the door divided by speed, which results in a higher number proportional to moveDirection's magnitude. I believe Z is the right size axis, though I may be wrong. Change to X if needed.
		local moveAmount	= Door.PrimaryPart.Size.Z / Speed;

		-- Move the door in moveDirection, moveAmount times.
		for i = 1, moveAmount do
			Door:SetPrimaryPartCFrame(
				Door.PrimaryPart.CFrame * moveDirection
			);

			wait(.0025);
		end;
		
		wait(.5);
		
		IsOpen	= not IsOpen;

		ToggleLights();

		Ready	= true;
	end;
end;

Do let me know if this works. If you have any questions (or experienced an error I overlooked,) feel free to let me know however you see fit. Have a nice day, and best of luck with your SCP game!

Best regards,

tkcmdr

Edit: Removed unnecessary comment, function call

1 comment

0

Don't forget to mark this question as answered, bud! c;

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