Hello, I am attempting to make a door that when interacted with a proximity prompt, it goes upwards using tweens, and if it is interacted with when it is up, it goes down. I have no idea as I am not experienced at coding. Replies are appreciated. Thanks
Now the ideal way to move models via scripting is PVInstance:PivotTo(), but this cannot be tweened. So we instead we can: Create a CFrameValue, tween it, then apply any changes to the model.
local TweenService = game:GetService("TweenService") function TweenModel(Model, Info, DesiredCFrame) local CFrameValue = Instance.new("CFrameValue") local Tween = TweenService:Create(CFrameValue, Info, {Value = DesiredCFrame}) CFrameValue.Changed:Connect(function(TweenedCFrame) Model:PivotTo(TweenedCFrame) end) Tween.Completed:Once(function() Tween:Destroy() CFrameValue:Destroy() end) Tween:Play() end
Insert a Part
into the model and position it to be at the centre of it. You will tween this Part
.
Set it's transparency to 1, CanCollide to false and anchor it.
(optional) Set the PrimaryPart of the model to the Part
.
Weld the entire model to the Part
. I usually use Moon Animator plugin's weld feature, but if you don't have it then you can manually weld the entire model using WeldConstraints and setting Part0 to the Part
.
Not sure where your ProximityPrompt is so I'll put it in the Part
.
Insert a ProximityPrompt
in the Part
, and set it's properties.
Insert a Script
in the Part
.
TweenService = game:GetService("TweenService") part = script.Parent prompt = part:WaitForChild("ProximityPrompt") duration = 5 -- time taken for the model to go up/down downPos = Vector3.new(0, 0, 0) -- position of the Part when it is down upPos = Vector3.new(0, 10, 0) -- position of the Part when it is up isUp = false tweenUp = TweenService:Create(part, TweenInfo.new(duration), {Position = upPos} tweenDown = TweenService:Create(part, TweenInfo.new(duration), {Position = downPos} prompt.Triggered:Connect(function(player) if isUp == false then tweenDown:Play() prompt.Enabled = false tweenDown.Completed:Wait() prompt.Enabled = true else tweenUp:Play() prompt.Enabled = false tweenUp.Completed:Wait() prompt.Enabled = true end end)
I typed everything on mobile so let me know if there's any errors.