Help with ViewportFrames

So I was reading this post, and I need a bit of help with something however I don’t wanna necropost it.

I’m using @EgoMoose’s script

local vpf = script.Parent:WaitForChild("ViewportFrame")
local items = script.Parent:WaitForChild("Items")
local children = items:GetChildren()

local index, event = 1, nil

local camera = Instance.new("Camera")
camera.FieldOfView = 70
vpf.CurrentCamera = camera
vpf.Visible = true


-- rotation

local function setRotationEvent(model)
	local currentAngle = 0
	local modelCF, modelSize = model:GetBoundingBox()	

	local rotInv = (modelCF - modelCF.p):inverse()
	modelCF = modelCF * rotInv
	modelSize = rotInv * modelSize
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local diagonal = 0
	local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
	local tan = math.tan(math.rad(camera.FieldOfView/2))

	if (maxExtent == modelSize.x) then
		diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
	elseif (maxExtent == modelSize.y) then
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
	else
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
	end

	local minDist = (maxExtent/2)/tan + diagonal

	return game:GetService("RunService").RenderStepped:Connect(function(dt)
		currentAngle = currentAngle + 1*dt*60
		camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)
	end)
end

local function setIndexActive(newIndex)
	if (event) then
		event:Disconnect()
		children[index].Parent = items
	end

	index = newIndex
	event = setRotationEvent(children[index])
	children[index].Parent = vpf
end

--

setIndexActive(index)

vpf.InputBegan:Connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
		setIndexActive(index + 1 > #children and 1 or index + 1)
	end
end)

How would I make it so the object spins when the mouse is hovering over the ViewportFrame, and stops when it leaves it?

edit: if i dont reply im probably busy :cowboy_hat_face:

I’ve made some progess

When the mouse leaves the frame, the rotation pauses, but when it enters, it restarts.

vpf.MouseEnter:Connect(function(input)
	setIndexActive(1)
end)

vpf.MouseLeave:Connect(function()
	if (event.Connected) then
		event:Disconnect()
	end
end)

Have you tried using a tween, and then :Pause() to pause the tween when the mouse leaves the frame? I’m not sure if it’s compatible though.

I haven’t, like I said I took the code from another post and I don’t know how I’d add a tween to it ¯\(ツ)

It’s this line here, if you converted this to a tween, and renamed the variable event to tween, it should then work as a proper tween.


Well that happened

local function setRotationEvent(model)
	local currentAngle = 0
	local modelCF, modelSize = model:GetBoundingBox()	

	local rotInv = (modelCF - modelCF.p):inverse()
	modelCF = modelCF * rotInv
	modelSize = rotInv * modelSize
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local diagonal = 0
	local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
	local tan = math.tan(math.rad(camera.FieldOfView/2))

	if (maxExtent == modelSize.x) then
		diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
	elseif (maxExtent == modelSize.y) then
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
	else
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
	end

	local minDist = (maxExtent/2)/tan + diagonal

	return game:GetService("RunService").RenderStepped:Connect(function(dt)
		currentAngle = currentAngle + 1*dt*60
		game:GetService('TweenService'):Create(camera, TweenInfo.new(
		1,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out), {CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)}):Play()
	end)
end

local function setIndexActive(newIndex)
	if (tween) then
		tween:Disconnect()
		children[index].Parent = items
	end

	index = newIndex
	event = setRotationEvent(children[index])
	children[index].Parent = vpf
end

--

vpf.MouseEnter:Connect(function(input)
	setIndexActive(1)
end)


a bit better

local vpf = script.Parent:WaitForChild("ViewportFrame")
local items = script.Parent:WaitForChild("Items")
local children = items:GetChildren()

local index, tween = 1, nil

local camera = Instance.new("Camera")
camera.FieldOfView = 70
vpf.CurrentCamera = camera
vpf.Visible = true


-- rotation

local function setRotationEvent(model)
	local currentAngle = 0
	local modelCF, modelSize = model:GetBoundingBox()	

	local rotInv = (modelCF - modelCF.p):inverse()
	modelCF = modelCF * rotInv
	modelSize = rotInv * modelSize
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local diagonal = 0
	local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
	local tan = math.tan(math.rad(camera.FieldOfView/2))

	if (maxExtent == modelSize.x) then
		diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
	elseif (maxExtent == modelSize.y) then
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
	else
		diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
	end

	local minDist = (maxExtent/2)/tan + diagonal

	return game:GetService("RunService").RenderStepped:Connect(function(dt)
		currentAngle = currentAngle + 1*dt*60
		game:GetService('TweenService'):Create(camera, TweenInfo.new(
		1,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out), {CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)}):Play()
	end)
end

local function setIndexActive(newIndex)
	if (tween) then
		tween:Disconnect()
		children[index].Parent = items
	end

	index = newIndex
	tween = setRotationEvent(children[index])
	children[index].Parent = vpf
end

--

vpf.MouseEnter:Connect(function(input)
	setIndexActive(1)
end)

vpf.MouseLeave:Connect(function()
	if (tween) then
		tween:Disconnect()
	end
end)

When I said convert it into a tween, I meant the entire RenderStepped event…