Hello all,
I’m trying to create an elevator for my game, but I’m not sure how to prevent players from clipping through the floor when the TweenService animation plays. (To clarify, I used the TweenService animator plugin.)
I would like to know if there is a way to anchor the player to the part (it is named “Elevator” so the player doesn’t clip.
animator = require(script.Parent.Elevator.Animator) script.Parent.Button.ClickDetector.MouseClick:Connect(function() script.Parent.Button.ClickDetector.MaxActivationDistance = 0 wait(0.1) script.Parent.Elevator.ElevatorWhirr:Play() wait(0.1) animator.ElevatorDown:Play() wait(10) script.Parent.Elevator.ElevatorWhirr:Play() wait(0.1) animator.ElevatorUp:Play() script.Parent.Button.ClickDetector.MaxActivationDistance = 32 end)
I'm not sure how to anchor the player to the part so they dont move until the script is done executing, can anyone show me how it works/point me in the right direction?
BodyPositions are basically Tweening but the player's character moves with it! I've seen with normal TweenService Tweening that it doesn't bring the player!
To save you time I made a small script for it! I don't know if it works but I don't know why it wouldn't!
-- Welding all the parts together for _, v in pairs(workspace:FindFirstChild("Elevator"):GetDescendants()) do if v:IsA("BasePart") then v.Anchored = false local weldConstraint = Instance.new("WeldConstraint") weldConstraint.Part0 = v weldConstraint.Part1 = workspace:FindFirstChild("Elevator"):FindFirstChild("ElevatorFloor") weldConstraint.Parent = workspace:FindFirstChild("Elevator"):FindFirstChild("ElevatorFloor") end end local bodyPosition = Instance.new("BodyPosition") bodyPosition.Parent = workspace:FindFirstChild("Elevator"):FindFirstChild("ElevatorFloor") -- Change that to the Elevator floor! bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge) bodyPosition.Position = workspace:FindFirstChild("Elevator"):FindFirstChild("ElevatorFloor").Position -- When the elevator moves! bodyPosition.D = 700 bodyPosition.Position = workspace:FindFirstChild("Elevator"):FindFirstChild("ElevatorFloor").Position + Vector3.new(0, 50, 0) -- Change to the position you want!
Try using WeldConstraint
. When the Tween is played, you create the WeldConstraint immediately.
Tween:Play() --you play the tween local weldConstraint = Instance.new("WeldConstraint")
Next, make sure your elevator model has a PrimaryPart
, or an invisible part in the middle of the model that connects all of the instances/parts inside that model. (like the HumanoidRootPart of the character)
The Part0
of the constraint should be the player's HumanoidRootPart or torso, and the Part1
should be the PrimaryPart of the elevator.
(if this script below doesn't work, try making the PrimaryPart of the model and edit the PrimaryPart of the model by clicking the model and click PrimaryPart under Pivot, and click the part or the middle invisible part of the elevator that serves as an anchor)
If you're lazy to read the above if the script below doesn't work (lol) then just edit the PrimaryPart in the script into any part from the model. (like turning weldConstraint.Part1 = model.PrimaryPart
to weldConstraint.Part1 = model."AnyPartNameHere"
)
weldConstraint.Part0 = (plr.Character or plr.CharacterAdded:Wait()):FindFirstChild("HumanoidRootPart") or (plr.Character or plr.CharacterAdded:Wait()):FindFirstChild("Torso") --the humrootpart or torso weldConstraint.Part1 = model.PrimaryPart --the elevator model weldConstraint.Parent = model.PrimaryPart weldConstraint.Name = "CharacterMeldWeld"
Then after that, add wait()
or task.wait()
and inside the parenthesis, add the length of the tween plays. (no difference between wait() and task.wait() but task.wait() is more precise and much recommend to use)
task.wait(--number of seconds how long your tween takes) --you can change this to wait() if you like
And after that, remove the WeldConstraint so the player can move freely. (the player cannot move if the elevator is going up or down because of WeldConstraint)
local theConsTraintWeldPart = model.PrimaryPart --or the part of the elevator model where you put the constraint in theConsTraintWeldPart.CharacterMeldWeld:Destroy()
You're done! I used this method in one of my games used for testing. (If you're wondering what game, here it is! https://web.roblox.com/games/9393617137/jiafei-horror-game-test and I'M NOT ADVERTISING MY GAME!)
Lemme know if there are errors or mistakes, or parts you don't understand. Have a good day! ^^