i had originally done this, but the code does not run again(when i tested) after the player Respawns/Re-Set.( at least when the script is in the StarterPlayerScripts)
Then add a Player.CharacterAdded. Youâre calling a CharacterAdded connection without returning anything.
Like this:
local Character = player.Character or Player.CharacterAdded:Wait()
Player.CharacterAdded:Connect(function(Char)
Character = Char
end)
I can still freely remove the camera. This is what I currently have as a local script.
âlocalscript in StarterPlayerScripts
local player = game.Players.LocalPlayer
local Character = player.Character or Player.CharacterAdded:Wait()
Player.CharacterAdded:Connect(function(Char)
Character = Char
end)
local Root = Character:WaitForChild(âHumanoidRootPartâ)
local lastZpos = Root.CFrame.Z
local RunService = game:GetService(âRunServiceâ)
RunService.Heartbeat:Connect(function()
if lastZpos ~= Root.CFrame.Z then
Root.CFrame = CFrame.new(Vector3.new(Root.Position.X, Root.Position.Y, lastZpos)) * CFrame.Angles(0, math.rad(Root.Orientation.Y),0) â limits the z axis, but maintains the orientation of the HumanoidRootPart
lastZpos = Root.CFrame.Z
end
end)
end)
You arenât setting the CameraType to Scriptable, therefore it doesnât set the camera position.
So, after playing around with the camera, i came up with this:
(@zQ86, good idea about the character added thing)
-------localScript in StarterCharacterScripts
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root= Character:WaitForChild("HumanoidRootPart")
local DistanceFromPlayer = 20 -- Distance from player
local lastZpos = Root.CFrame.Z
local lastXpos = Root.CFrame.X
local XLocked = true -- X axis Locked?
local ZLocked =false-- Z axis Locked?
local Direction = 0
local function SetCameraDirection(Setinverse)
if Setinverse then
DistanceFromPlayer = -DistanceFromPlayer
if XLocked and not ZLocked then
Direction = -90
else
Direction = 180
end
else
DistanceFromPlayer = math.abs(DistanceFromPlayer)
if XLocked and not ZLocked then
Direction = 90
else if ZLocked and not XLocked then
Direction = 0
end
end
end
end
SetCameraDirection()
Camera.CameraType = Enum.CameraType.Scriptable
RunService:BindToRenderStep("CameraPosition", Enum.RenderPriority.Camera.Value, function()
if XLocked and not ZLocked then
Camera.CFrame = CFrame.new(Vector3.new( Root.Position.X + DistanceFromPlayer , Root.Position.Y + 5 , Root.Position.Z )) * CFrame.Angles(0,math.rad(Direction), 0)
else
Camera.CFrame = CFrame.new(Vector3.new( Root.Position.X , Root.Position.Y + 5 , Root.Position.Z + DistanceFromPlayer )) * CFrame.Angles(math.rad(-10) ,math.rad(Direction) , 0)
end
end)
RunService.Heartbeat:Connect(function()
if lastZpos ~= Root.CFrame.Z and ZLocked then
Root.CFrame = CFrame.new(Vector3.new(Root.Position.X, Root.Position.Y, lastZpos)) * CFrame.Angles(0, math.rad(Root.Orientation.Y),0) -- limits the z axis, but maintains the orientation of the HumanoidRootPart
lastZpos = Root.CFrame.Z
end
if lastXpos ~= Root.CFrame.X and XLocked then
Root.CFrame = CFrame.new(Vector3.new(lastXpos, Root.Position.Y, Root.Position.Z)) * CFrame.Angles(0, math.rad(Root.Orientation.Y),0)
lastXpos = Root.CFrame.X
end
end)
Player.CharacterAdded:Connect(function(Character)
Character = Character
end)
Using This Method, this is the result:
Also i highly recommend you look into the documentation for all of this stuff(wiki) if you donât understand something!
Thank you Jaycbee05! It works perfectly. However, if I die, the camera goes off, and my character can freely move. How do I fix that?
While there are a couple ways of fixing that issue, i find that putting the script in StarterCharacterScripts
is one of the easier ways to do it, instead of putting it in the StarterPlayerScripts
. i probably should have put that in my previous post