Using the VectorForce to move a sliding door [RESOLVED]

Hello everyone, so, I am making a sliding door with VectorForce, because it seems to me more realistic. I tried it manually and as you can see it works pretty well.

However to avoid doing a script per door, I started to make a single script for all doors. Everything works except one thing, the direction of the vector. I cannot manage the vector properly and the door does not slide. Either it goes forward or backward but never from right to left. No matter how I change the settings, nothing works. I need your help, thank you in advance.

I’m sorry for my bad english.

script:

local Open = false
local Close = true

for i, v in ipairs(workspace:GetChildren()) do

if v.Name == ("DoorSlideS") then
	

	v.ClickDetector.MouseClick:Connect(function()

--Open the door

if Open == false then
		
v.ClickDetector.MaxActivationDistance = 0

-- Create Attachment
			
local Attachment = Instance.new("Attachment")
Attachment.Name = ("Attachment0")
Attachment.Parent = v.VectorOpen
Attachment.WorldPosition = v.VectorOpen.Position
Attachment.Orientation = Vector3.new(0,90,0)


v.AnchoredPart.Anchored = false

-- Create the VectorForce
	
local VectorForce = Instance.new("VectorForce")
VectorForce.Enabled = true
VectorForce.Parent = v.VectorOpen
VectorForce.ApplyAtCenterOfMass = true
VectorForce.Force = Vector3.new(0, 0, 500)	
VectorForce.Attachment0 = Attachment


Open = true

wait(2)

VectorForce:Destroy()
Attachment:Destroy()
v.AnchoredPart.Anchored = true
v.ClickDetector.MaxActivationDistance = 5


--Close the door

elseif Open == true then
	
	v.ClickDetector.MaxActivationDistance = 0
	
	-- Create Attachment
	
local Attachment = Instance.new("Attachment")
Attachment.Name = ("Attachment0")
Attachment.Parent = v.VectorClose
Attachment.Orientation = Vector3.new(0,-90,0)


v.AnchoredPart.Anchored = false

--Create VectorForce

local VectorForce = Instance.new("VectorForce")
VectorForce.Enabled = true
VectorForce.Parent = v.VectorClose
VectorForce.ApplyAtCenterOfMass = true
VectorForce.Force = Vector3.new(0, 0, 500)
VectorForce.Attachment0 = Attachment	
Open = false

wait(2)
VectorForce:Destroy()
Attachment:Destroy()
v.AnchoredPart.Anchored = true
v.ClickDetector.MaxActivationDistance = 5
	

	end
end)

	end
end

Hey there, I would actually use Tweening in your case. It keeps the smoothness while also allowing easy CFrame manipulation. What I would do in your case is have a part face the direction you want the door to slide. Then get the door relative to the object and have it always move away from it. That way, you can rotate the door without it breaking and going in the wrong direction.

Cheers

2 Likes

Hi thank you for your answer! but I would like a single script to move all the doors in my game, regardless of their size or direction

Precisely. If you were to do this with your doors, you could have a single script. Each time a door is clicked, the script grabs the little orientation block. Then you get its look vector. From there you would CFrame Tween the door in the lookvector’s direction of the orientation brick. Thus you could rotate the door in any way and it will still work

But imagine that a door must have less distance to go than another door does not work?

I mean you could have another brick called end brick and it tweens between the 2? So it starts at start brick, and it tweens to the endbrick position. To be honest that would probably be even easier than getting relative CFrame. You could do something like this

local TweenService = game:GetService("TweenService")

for i,v in pairs(workspace:GetChildren()) do
    if v.Name == "MoveableDoor" then
        v.ClickPart.ClickDetector.Clicked:Connect(function()
            local startpos = v.StartBrick.Position
            local endpos = v.EndBrick.Position
            local newtween = TweenService:Create(v.DoorMain, Tweeninfo.new(3), {Position = endpos}
            newtween:Play()
            newtween.Completed:Wait()
            wait(2)
            newtween = TweenService:Create(v.DoorMain, Tweeninfo.new(3), {Position = startpos}
            newtween:Play()
            newtween.Completed:Wait()
        end)
     end
end

This is just example code. You would have to modify it for your use case.

The easiest option is to just change the ‘RelativeTo’ setting to Attachment0, and then set Attachment0 of your vector force to an attachment inside the door facing the direction you want it to open.

This might be useful for you to know later, so I’ll include this option too, provided you have a part in your door that is always oriented the same relative to the direction the door opens, you can use CFrame:VectorToObjectSpace to find the appropriate direction. For example:
Force = Part.CFrame:VectorToObjectSpace(Vector3.new(0,0,1)) * 100
Depending on the direction the part is facing, you may need to change it to a -1 or put the ±1 in the X or Y component instead. VectorToObjectSpace takes a global vector, and returns what that vector would look like from the perspective of the given CFrame, for example:
CFrame:VectorToObjectSpace(Vector3.new(0,0,-1))
Is the same as
CFrame.LookVector
But understanding how this method works is useful because it’s more versatile.

To be truthful though, you should probably just use a prismatic constraint instead and set ‘ActuatorType’ to ‘Servo’

4 Likes

Ok I see, I am very inexperienced. I didn’t even know TweenService. Thank you very much I will try!

Hi thank you for your answer, I did not understand everything, as I said I do not know much but I will resign myself on what you advise me!