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

Click Detector to tween a model?

Asked by 5 years ago
Edited 5 years ago

So I've tried making my model move upwards slowly by using a clickdetector. I can make it teleport with the MainPart property but that's the best I can do.

button = script.Parent
model = script.Parent.Parent.Parent.Gate

button.ClickDetector.MouseClick:connect(function()
    model:MoveTo(Vector3.new(-22.95, 33.3, 148.5))
end)

How would I make it go upwards slowly rather than have it teleport upwards? This sort of works but is far from good. Any help is appreciated.

0
What is the question? What is the problem with the code? Miniller 562 — 5y
0
The problem is trying to get it to go upwards smoothly. I'm not sure how I would tween a model. Sergeant_Hale 0 — 5y
0
Best way to tween models is to have a non collidable, invisible and anchored part as a "root" and weld the model to that part via a Motor6D, unanchoring the model later. Then you tween root with a tweenservice. sleazel 1287 — 5y
0
Thank you so much. Sergeant_Hale 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

To do this, you would use the SetPrimaryPartCFrame function of models and the lerp function of cframe, and of course, a handy for loop.


SetPrimaryPartCframe and Lerp


The set primary part cframe function differs from directly setting a model's primary part in the manner that it moves the entire model rather than just the primary part.

Lerp, or linear interpolation is a way of smoothing changing an object's cframe between two points, the basic function of lerping can be summarized here:

local function lerp(a,b,alpha)
    return a + (b-a) * alpha
end

Application


With that said, you can do the following:

button = script.Parent
model = script.Parent.Parent.Parent.Gate
local pos = Vector3.new(-22.95, 33.3, 148.5)
local start = model:GetPrimaryPartCFrame()

button.ClickDetector.MouseClick:Connect(function()
    for a = 0,1,0.05 do
        local p = cframe.lerp(start,CFrame.new(pos),a)
        model:SetPrimaryPartCFrame(p)
        wait()
    end
end)

Hopefully this helped!

0
Been trying different methods (including this) for 30 mins or so now. It's just not working. No errors or anything. Thank you anyway. Sergeant_Hale 0 — 5y
0
does the model have a primary part? theking48989987 2147 — 5y
Ad

Answer this question