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

How do I make a elevator model move?

2 answers

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:

-- Models
local elevatorModel = script.Parent
local elevator = elevatorModel:WaitForChild("Elevator")
-- Pars
local mainPart = elevator:WaitForChild("MainPart")
local floor1 = elevatorModel:FindFirstChild("Floor1")
local floor2 = elevatorModel:FindFirstChild("Floor2")
-- Services
local TweenService = game:GetService("TweenService")
-- Variables
local waitTime = 2
-- Functions
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)
	-- Changing the power and dampening effect of the bodyPosition
	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")
	-- Check if the mainPart is lower than the floor, if it is then wait until it gets higher
	if whichPart.Position.Y < targetPart.Position.Y then
		while wait(0.1) do 
			if whichPart.Position.Y >= targetPart.Position.Y then
				-- Stop the elevator from moving any higher
				bodyPosition.Position = whichPart.Position
				print("Elevator reached top")
				return
			end
		end
	else
		-- If the mainsPart is higher than the floor, then wait until it gets lower than the floow
		while wait(0.1) do
			if whichPart.Position.Y <= targetPart.Position.Y then
				-- Stop the elevator from moving any lower
				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")
		-- We change the bodyPosition to the position of floor2 and make it go a little higher just so that its position is higher than our target
		bodyPosition.Position = floor2.Position + Vector3.new(0,1,0)
		WaitForPartToReachPosition(mainPart, floor2)
		-- Adding a custom wait so the elevator doesn't instantly go back down
		wait(waitTime)
		MoveElevator(-1)
	else
		print("Moving elevator down")
		-- We change the bodyPosition to the position of floor1 and make it go a little lower just so that its position is lower than our target
		bodyPosition.Position = floor1.Position - Vector3.new(0,1,0)
		WaitForPartToReachPosition(mainPart, floor1)
		-- Adding a custom wait so the elevator doesn't instantly go back down
		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!

Hello there.

Since your script isn't working, you could use Vector3 to move it smoothly. But in order to do that, you have to union the elevator.

But if you are having problems with the collision of the union, change the CollisionFidelity to PreciseConvexDecomposition.

https://gyazo.com/455e3d99a03c5cdaf25fe8da0be72993

Then put these lines of code in the script:

local Elevator = script.Parent.Elevator
local Button = script.Parent.Button
local Debounce = false
function Up()
Elevator.Position = Elevator.Position + Vector3.new(0,.1,0)
end
function Down()
Elevator.Position = Elevator.Position - Vector3.new(0,.1,0)
end
Button.ClickDetector.MouseClick:Connect(function()
if Debounce == false then
Debounce = true
Button.ClickDetector.MaxActivationDistance = 0		
for i = 1,50 do
Up()
wait()
end
wait(7)
for i = 1,50 do
Down()
wait()
end
Button.ClickDetector.MaxActivationDistance = 50
Debounce = false
end
end)

You're welcome, and have fun.

1 comment

0

I think you need to use local before the function up() and function down()

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