How do i rotate an object inside a viewport frame in a server script?

I want to rotate some items inside a viewport frame, yes, there is already a post about this, but that’s client sided, and i want my current code server sided. But i don’t know how can i do that, i don’t know if i should use a while true do changing the rotation of the object, which i don’t know how to do it, thanks for reading.

1 Like

Not only should you not be doing this from the server irregardless of your reasoning, but you shouldn’t be rotating the actual object in a ViewportFrame either. It is documented to be more expensive to update the model than it is to update the Camera CFrame.

Personally I would bind the function to Heartbeat rather than to use a while loop or just an infinitely repeating tween. The idea is that you will want to be CFraming your camera to a certain point, pushing the camera back a few studs (such as 5) and then rotating it by a certain angle around.

6 Likes

colbert2677 is absolutely correct about doing it via camera, and on the client side.

Here’s what my code looks like:

--- Handles specific update of prop model
-- @classmod PropViewportModel
-- @author Quenty

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local RunService = game:GetService("RunService")

local BaseObject = require("BaseObject")
local CameraUtils = require("CameraUtils")

local RPS = 0.2
local TAU = math.pi*2

local ANGLE_DOWN = CFrame.Angles(-math.pi*(1/6), 0, 0)

local PropViewportModel = setmetatable({}, BaseObject)
PropViewportModel.ClassName = "PropViewportModel"
PropViewportModel.__index = PropViewportModel

function PropViewportModel.new(viewportFrame, model)
	local self = setmetatable(BaseObject.new(model), PropViewportModel)

	self._viewportFrame = viewportFrame or error("No viewportFrame")
	self._model = model or error("No model")

	self._maid:GiveTask(self._model)

	-- Init model
	self._model.Parent = self._viewportFrame
	self:_calculateBoundingBox()
	self:_updateViewportFrame()

	-- Do animation
	self._maid:GiveTask(RunService.RenderStepped:Connect(function()
		self:_updateViewportFrame()
	end))

	return self
end

function PropViewportModel:_calculateBoundingBox()
	self._camera = self._viewportFrame.CurrentCamera
	local cframe, size = self._obj:GetBoundingBox()

	local frameSize = self._viewportFrame.AbsoluteSize
	local aspectRatio = frameSize.x/frameSize.y
	self._center = CFrame.new(cframe.p)
	self._totalDistBack = CameraUtils.fitBoundingBoxToCamera(size, self._camera.FieldOfView, aspectRatio)
end

function PropViewportModel:_getOffset()
	return CFrame.Angles(0, (tick()*RPS*TAU) % TAU, 0)
		* ANGLE_DOWN
end

function PropViewportModel:_updateViewportFrame()
	local offset = self:_getOffset()
	self._camera.CFrame = self._center * offset * CFrame.new(0, 0, self._totalDistBack)
	self._camera.Focus = self._center
end

return PropViewportModel
2 Likes

How i would rotate the camera around something else?

There are only a few times when you should go to the effort of hiding code server-side, and it’s usually code that belongs on the server in the first place, like code that contains authentication credentials, or code that would reveal how to find/solve things in your game that users are meant to discover or work out on their own (gameplay secrets). UI code, and visual effects code should be client side and you shouldn’t degrade performance of your game to try to hide it. Something as basic as code to rotate a model is not a candidate for being a trade secret.

1 Like

Why I should handle it on the client?


@EmilyBendsSpace Then how I would do this?