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

How do I tween a model?

Asked by 1 year ago

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

2 answers

Log in to vote
0
Answered by
wf_sh 15
1 year ago

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
0
how do i change the DesiredCFrame though? xbantixx 10 — 1y
0
Do you lack an understanding of functions, or are you unsure of how to find the CFrame y studs above or below the door? wf_sh 15 — 1y
0
first one :/ xbantixx 10 — 1y
0
It's alright, the Roblox documentation provides a nice explanation. https://create.roblox.com/docs/scripting/luau/functions wf_sh 15 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

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.

Answer this question