Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I anchor the player to the part to prevent clipping?

Asked by 1 year ago
Edited 1 year ago

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?

0
Try using BodyPositions! MattVSNNL 620 — 1y

2 answers

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
1 year ago

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!
0
Thank you for trying to help, but I'm not exactly sure where to put this script. I tried making a new script in the same model and editing what is needed, but the player did not anchor to the elevator part. I should let you know, that this is more of a lift than an elevator. Its a single part that goes down when a button is pressed through the use of TweenService - kingsworld9 14 — 1y
0
- and back up again automatically again through the use of TweenService. I used the TweenService animator plugin, and this "elevator" only has 2 floors. One, the top, and 2, the bottom. kingsworld9 14 — 1y
0
There are no "ElevatorFloors" as it is simply a "go down, come back up" after a button is pressed. What I want to know is how the player can be anchored BEFORE this script executes, so they will be anchored for the remainder of the script duration and then unanchored when the script ends. kingsworld9 14 — 1y
0
maybe make the player unable to move (like set their speed to 0) then use a loop that make sures the player's position is always greater or equal to the elevator's position to prevent them clipping ZeroToH3ro 82 — 1y
View all comments (3 more)
0
Uhmm BodyPosition is deprecated.. T3_MasterGamer 2189 — 1y
0
Use the script I sent MattVSNNL 620 — 1y
0
Also the script can be anywhere MattVSNNL 620 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

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! ^^

Answer this question