How would I make this Camera-Script follow the Torso again after the player dies?
Currently it works when the player has not died yet, but when I reset, the camera stays in it's place, and I can freely move my player again too! (Which is not supposed to happen because of the BodyPositioner)
So, my question is, how would I kind of "reset" everything (The camera and the BodyPositioner) back to how they were before the player reset? (So that the Camera is following the Torso of the player, and that the BodyPositioner works again?)
I have tried everything from rebuilding the script to adding more code, but nothing seems to help!
Here is the coding, it is in a LocalScript in StarterPlayerScripts:
local Camera = game.Workspace.CurrentCamera local Player = game.Players.LocalPlayer repeat wait() until Player.Character local Character = Player.Character local Player_Torso = Character.Torso local BodyPositioner = Instance.new("BodyPosition", Player_Torso) BodyPositioner.MaxForce = Vector3.new(0, 0, math.huge) BodyPositioner.Position = Vector3.new(0, 0,Player_Torso.Position.Z) ------------------------------------------------------------------- Camera.CameraType = Enum.CameraType.Scriptable Camera.CameraSubject = Player_Torso game:GetService("RunService").RenderStepped:connect(function() Camera.CFrame = CFrame.new(Player_Torso.CFrame.X, Player_Torso.CFrame.Y, Player_Torso.CFrame.Z + 30) Camera.FieldOfView = 80 end)
You will need to get Player_Torso
again every time the player respawns. Along with that, you'll also need to re-insert your BodyPositioner
. To do that, you will have to listen to the CharacterAdded
event of the player. Here's the rewritten code -- I'll annotate my changes.
local Camera = game.Workspace.CurrentCamera local Player = game.Players.LocalPlayer -- Get the player's character, or wait for a new one to spawn. local Character = Player.Character or Player.CharacterAdded:Wait() -- Renamed Player_Torso to Torso. It's uncommon style to use underscores in Lua, -- especially when naming variables. local Torso = Character:WaitForChild("Torso") -- This function will be called every time the player's character spawns. function InitializeCharacter(character) -- Update the Torso and Character variable -- so that the camera locks on to the player's current Torso. Character = character Torso = character:WaitForChild("Torso") local BodyPositioner = Instance.new("BodyPosition", Torso) BodyPositioner.MaxForce = Vector3.new(0, 0, math.huge) BodyPositioner.Position = Vector3.new(0, 0, Torso.Position.Z) end -- Make a connection to the CharacterAdded event. Player.CharacterAdded:Connect(InitializeCharacter) -- Initialize the Character we have right now. InitializeCharacter(Character) Camera.CameraType = Enum.CameraType.Scriptable -- Unless you plan on using CameraSubject in some other way, -- Setting the CameraSubject does absolutely nothing in a Scriptable camera. -- Just leaving it in here just in case. Camera.CameraSubject = Torso -- Just set the camera's field of view once. Camera.FieldOfView = 80 game:GetService("RunService").RenderStepped:connect(function() -- Make sure that the Torso hasn't been destroyed. if Torso and Torso:IsDescendantOf(workspace) then -- This is more clear than your current code. -- Moves the camera 30 studs away from the torso. Camera.CFrame = CFrame.new(Torso.Position + Vector3.new(0, 0, 30)) end end)