Elevator welds not working properly

Hello. I am trying to make an elevator/lift using TweenService and WeldConstraints. However, sometimes the player just doesn’t get welded, or when the player tries to get out of the elevator/lift, they get teleported back to the lift.

THERE ARE NO ERRORS IN THE OUTPUT

-- Elevator

-- setup
local tween = game:GetService("TweenService")
local elevator = script.Parent
local canopen = true

local notMoving = false


-- tween setup
local eletweeninfo = TweenInfo.new(
	10,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.InOut,
	0,
	false,
	0)


script.Parent.Touched:Connect(function(hit)
	
	wait(1)
	if hit.Parent.ClassName == "Accessory" then
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = elevator
		weld.Part1 = hit.Parent.Parent.HumanoidRootPart
		weld.Parent = hit.Parent.Parent.HumanoidRootPart
		repeat wait() until notMoving == true
		hit.Parent.Parent.HumanoidRootPart.WeldConstraint:Destroy()
	else
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = elevator
		weld.Part1 = hit.Parent.HumanoidRootPart
		weld.Parent = hit.Parent.HumanoidRootPart
		repeat wait() until notMoving == true
		hit.Parent.HumanoidRootPart.WeldConstraint:Destroy()
	end
	
end)


local TweenUp = tween:Create(elevator, eletweeninfo, {CFrame = elevator.Parent.TweenUpFloor.CFrame}) --tween "animation"
local TweenDown = tween:Create(elevator, eletweeninfo, {CFrame = elevator.Parent.TweenDownFloor.CFrame})



while true do 
	wait(5)
	TweenDown:Play()
	wait(10)
	notMoving = true
	wait(5)
	notMoving = false
	TweenUp:Play()
	wait(10)
	notMoving = true
end

Can anyone help?

Seems like a problem with the way you’re using the touched event. Your debounce “notMoving” is being changed when the elevator stops moving, but it should actually be separate and handled in the touched function instead.

How would I handle it in the touched function? I am stumped.

You would add a return statement that would run if the condition of the variable aforementioned is not true. You would also wait a later amount of time before changing the variable and playing the tween.

If I add a return statement the code will just end meaning the player will be forever welded to the elevator and unable to move. I tried it.

I should have clarified further. The return statement should only return if the new variable is true. You would return at the top of the touched event too, so the player does not get welded in any case where they won’t be unwelded.

Another possibility is to have a debounce. You would set this to true after the repeat statement ends, keep it at true for a second or two, then make it false. You would return the statement if it is true.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.