How do I update CFrame when a player moves

  1. What do you want to achieve? Keep it simple and clear!
    I want forever updating CFrame, like I have made Camera that is welded to player’s head, everything is fine, but now I want to update the CFrame when the player moves, just like first-person mode

  2. What is the issue? Include screenshots / videos if possible!
    I tried a lot of solutions, but nothing worked.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I was researching, finding the solution on my own, but nothing worked.

Here’s the local script that I’m working on. This game will be 1 player only.
The camera shows POV of the player’s head, but doesn’t updates. I tried with while, run services, but I have no clue.

local players = game:WaitForChild("Players")
local player = players.LocalPlayer
local chr = player.Character

local CCamera = Instance.new("Part")
CCamera.Name = "Camera"
CCamera.Parent = chr.Head
CCamera.Position = Vector3.new(chr.Head.Position.X, chr.Head.Position.Y, chr.Head.Position.Z)
CCamera.Anchored = false
CCamera.CanCollide = false

game:WaitForChild("Workspace").CurrentCamera.CameraType = Enum.CameraType.Scriptable

local newWelder = Instance.new("WeldConstraint")
newWelder.Parent = chr
newWelder.Part0 = CCamera
newWelder.Part1 = chr.Head
CCamera.Position = chr.Head.Position


game:WaitForChild("Workspace").CurrentCamera.CFrame = CCamera.CFrame

I have never seen someone use WaitForChild to get a Service.

Why are you overcomplicating it? Cant you just do this?

CCamera.Position = chr.Head.Position

or:

CCamera.CFrame = chr.Head.CFrame

I basically did :WaitForChild(), not because I wanted but I didn’t mind it when I was writing since I’m used that the first thing pops out is :GetService(), I will change tho.

It seems as though you may be unfamiliar or new to RLua, but I will attempt to explain a simple way that you could achieve this.

HumanoidStateType (a property of all Humanoids in the workspace) you can detect when a players HumanoidState has changed by using Humanoid.StateChanged, this will fire an event whenever the player has done something such as walking, running, jumping, etc, etc. Then a parameter can be passed that directly relates to the Humanoids current state which will act as a variable. Inside of the event you can use that parameter and add a check to see if it is equal to the running state if state == enum.HumanoidStateType.Running then

This will detect when a player has began to walk where you can then affect the CFrame

Use the Roblox Creator Documentation on these subjects for a more in-depth explanation with some handy examples:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.