Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Click Detector to tween a model?

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.

4 comments

0

What is the question? What is the problem with the code?

0

The problem is trying to get it to go upwards smoothly. I'm not sure how I would tween a model.

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.

0

Thank you so much.

1 answer

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!

2 comments

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.

0

does the model have a primary part?

This discussion is preserved as it was. New answers, comments, and votes are closed.