When you change a part's position or CFrame which is intended to move a character with it, the character won't move properly with it. Especially if the part is moving in the X or Z axis.
For that reason, I recommend either using a BodyPosition or a BodyVelocity to move the part so that the character moves along with it where ever the part goes.
Here's what I did to create a working elevator. I put two invisible parts and gave their name their respective floor. Next, I created an elevator model and I welded every part to a main part ("Which could be the floor of the elevator). You can use weld constraints to make sure the welded parts stay in the same relative position to one another. If you don't need your elevator to have multiple separate parts, then you could simply union all the part together.
Once you have your elevator model and your floors set, all that is left to do is add a BodyPosition to the main part of your elevator as well as a BodyGyro so that it may keep it's orientation.
Now you simply have to change the "BodyPosition" of the main part to the position of the floor part you want to move the elevator to.
I made a loop so that my elevator goes up and down. Here's what it looks like: https://gyazo.com/bc072164e8baf47e82b17dc8f379cf5f
Here's how it looks like in my explorer: https://prnt.sc/vhes2v
Here's what my elevator script looks like:
local elevatorModel = script.Parent
local elevator = elevatorModel:WaitForChild("Elevator")
local mainPart = elevator:WaitForChild("MainPart")
local floor1 = elevatorModel:FindFirstChild("Floor1")
local floor2 = elevatorModel:FindFirstChild("Floor2")
local TweenService = game:GetService("TweenService")
local waitTime = 2
local function CreateBodyPosition(whichPart)
if not whichPart then return end
local newBodyPosition = Instance.new("BodyPosition", whichPart)
newBodyPosition.Position = whichPart.Position
newBodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
newBodyPosition.P = 2000
newBodyPosition.D = 750
end
local function CreateBodyGyro(whichPart)
if not whichPart then return end
local newBodyGyro = Instance.new("BodyGyro", whichPart)
newBodyGyro.CFrame = mainPart.CFrame
newBodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
end
local function PrepareElevator()
CreateBodyPosition(mainPart)
CreateBodyGyro(mainPart)
mainPart.Anchored = false
end
local function WaitForPartToReachPosition(whichPart, targetPart)
local bodyPosition = whichPart:FindFirstChild("BodyPosition")
print("Checking if elevator reached target floor")
if whichPart.Position.Y < targetPart.Position.Y then
while wait(0.1) do
if whichPart.Position.Y >= targetPart.Position.Y then
bodyPosition.Position = whichPart.Position
print("Elevator reached top")
return
end
end
else
while wait(0.1) do
if whichPart.Position.Y <= targetPart.Position.Y then
bodyPosition.Position = whichPart.Position
print("Elevator reached bottom")
return
end
end
end
end
local function MoveElevator(upOrDown)
local bodyPosition = mainPart:FindFirstChild("BodyPosition")
local bodyGyro = mainPart:FindFirstChild("BodyGyro")
if upOrDown >0 then
print("Moving elevator up")
bodyPosition.Position = floor2.Position + Vector3.new(0,1,0)
WaitForPartToReachPosition(mainPart, floor2)
wait(waitTime)
MoveElevator(-1)
else
print("Moving elevator down")
bodyPosition.Position = floor1.Position - Vector3.new(0,1,0)
WaitForPartToReachPosition(mainPart, floor1)
wait(waitTime)
MoveElevator(1)
end
end
for i = 1,1 do
PrepareElevator()
MoveElevator(1)
end
If you're interested to know how I'm shooting balls that explodes, you can head over to this post: https://scriptinghelpers.org/questions/115567/rocket-propulsion-to-position
Let me know how it goes!
1 comment
The Tween isn't changing anything