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

What's a better way to approach this than the method I'm doing?

Asked by 4 years ago

Hello everyone!

I am making a camera script that follows the player, but at a set offset.

The execution for this seems pretty straightforward, as I have done this:

--This replaces the CameraScript found in StarterPlayerScripts

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

local offset = Vector3.new(0,10,15)

local humanoidRoot = character:WaitForChild("HumanoidRootPart")

while true do
    local playerPos = humanoidRoot.Position 

    local cameraCFrame = CFrame.new(playerPos + offset, humanoidRoot.Position)
    camera.CFrame = cameraCFrame

    wait()
end

This method WORKS, however It's a little bit choppy as seen in this example.

Now this seems to be due to the fact that the loop has the wait element in this, meaning that the script stops very momentarily and isn't on a constant loop. However removing this wait element breaks the script entirely.

Is there a different way to approach this to yield the same result that I haven't thought of?

Any help would be appreciated! Thank you :)

1 answer

Log in to vote
3
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can use RunService's .Heartbeat signal to run a block of code every Frame. This should allow you to properly render.

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
    --// Code
end)
Ad

Answer this question