Any tweaks/optimizations to this client-sided lift system?

This script is located in StarterPlayerScripts, and utilizes the ZonePlus module by @ForeverHD. Whenever a lift is used, it will make the other lifts move in the same direction. My end goal was to make one function that will work for the lift being used and the other lifts that are moving unoccupied.

local TweenService = game:GetService('TweenService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Zone = require(ReplicatedStorage:WaitForChild('Zone'))

local Lifts = workspace:WaitForChild('Lifts')

local Colors = {
	Default = ColorSequence.new(Color3.fromRGB(255, 255, 255)),
	Occupied = ColorSequence.new(Color3.fromRGB(0, 0, 0))
}

local function SetBeamColors(Platform, NewColor)
	for _, Beam in Platform:GetChildren() do
		if Beam:IsA('Beam') then
			Beam.Color = NewColor
		end
	end
end

local function MoveLift(Lift, Callback)
	local Platform = Lift.Platform
	local NewStatus = Lift:GetAttribute('Status') == 'Down' and 'Up' or 'Down'

	local Tween = TweenService:Create(Platform, TweenInfo.new(2), {
		Position = NewStatus == 'Down' and Lift:GetAttribute('OriginalPos') or Lift:GetAttribute('LiftedPos')
	})

	Tween:Play()

	Tween.Completed:Once(function()
		Lift:SetAttribute('Status', NewStatus)
		
		Callback()
	end)

	SetBeamColors(Platform, Colors.Occupied)
end

local function InitLift(Lift)
	local Platform = Lift:WaitForChild('Platform')

	local LiftZone = Zone.new(Lift:WaitForChild('Container'))

	LiftZone.localPlayerEntered:Connect(function()
		local Character = script.Parent.Parent.Character
		local Humanoid = Character.Humanoid

		Humanoid.WalkSpeed = 0
		Humanoid.JumpHeight = 0
		
		while Humanoid.MoveDirection.Magnitude > 0 do
			Humanoid:GetPropertyChangedSignal('MoveDirection'):Wait()
		end
		
		Character:PivotTo(Platform.CFrame + Vector3.new(0, 2.5))

		task.delay(0.5, function()
			MoveLift(Lift, function()
				Humanoid.WalkSpeed = 16
				Humanoid.JumpHeight = 7.2
			end)

			for _, Child in Lifts:GetChildren() do
				if Child ~= Lift then
					MoveLift(Child, function()
						SetBeamColors(Child.Platform, Colors.Default)
					end)
				end
			end
		end)

		SetBeamColors(Platform, Colors.Occupied)
	end)

	LiftZone.localPlayerExited:Connect(function()
		SetBeamColors(Platform, Colors.Default)
	end)

	local OriginalPos = Platform.Position

	Lift:SetAttribute('OriginalPos', OriginalPos)
	Lift:SetAttribute('LiftedPos', OriginalPos + Vector3.new(0, 23.8))
end

for _, Lift in Lifts:GetChildren() do
	task.defer(InitLift, Lift)
end
2 Likes

Couldn’t you just weld all the other platforms (Unanchored of course) to the Anchored one that you are Tweening?

Is this possible with the setup I have? I thought the parts have to be touching to weld.

I think you dont have to do anything of that. If you have the beams attachment0 and attachment1 parented to the lift part they should just stay positioned relative to the lift part

I’m not sure you are understanding, the beams move with the lift just fine. He was referring to the other lifts moving with the one that’s occupied (as seen in the video).

1 Like

All you need are WeldConstraints.

So you’re suggesting I weld all the lifts together?

If they all move at the same time then why not.
No extra scripting needed!

I’m not very well-versed with welds so I’ll have to mess with it a bit, I just added a a MoveDirection check for the player to ensure they aren’t moving before it attemps to pivot them.

Welds are easy.
Click the Part you want the WeldConstraint to start in.
Select the Constraints > Weld tool in the dropdown.
Select the other Part you want welded.

Or:
With nothing selected in the workspace click the Constraints > Weld tool then select the first and second Parts you want welded.

The nice thing about WeldConstraints is that you can Move or Rotate one or both Parts and the Weld won’t break, it’ll just reset to the new position.

I’ve made a few platform and elevator effects using PrismaticConstraints that work really well with players, since they rely on physics so a player can walk/move/jump on them and they won’t slide off while it’s moving. If you want to have a look you can check out my Steampunk place and wander around. I only used CFrame movement on a couple of the items there. All the elevators, doors, ramps, fans and moving platforms use Constraints.