Hello! How’s it going? Can you help me to make this script reset when a player dies? Thanks!
function onTouched(hit)
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
camera.CameraType = Enum.CameraType.Scriptable
local targetDistance = 30
local cameraDistance = -30
local cameraDirection = Vector3.new(-1,0,0)
local currentTarget = cameraDirection*targetDistance
local currentPosition = cameraDirection*cameraDistance
game:GetService("RunService").RenderStepped:connect(function()
local character = player.Character
if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
local torso = character.HumanoidRootPart
camera.Focus = torso.CFrame
if torso:FindFirstChild("FastStart") == nil then
camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y + 10, torso.Position.Z - 20) + currentPosition,
Vector3.new(torso.Position.X, torso.Position.Y, torso.Position.Z - 20) + currentTarget)
else
camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentPosition,
Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentTarget)
end
end
end)
script.Parent.Touched:connect(onTouched)
end
If you place the script in StarterCharacterScripts, a new copy will be cloned into the character every time their character is reloaded (e.g. when they die).
Can you elaborate as to what you mean by “reset”? If the script is in StarterCharacterScripts, it should be generating a new copy of it and parenting that new copy to the PlayerScripts folder each time the player’s Character is reloaded.
If i fully understand, your camera’s cframe isn’t set to the desired position, does it reset to the normal camera, are there any errors? does the camera just stay in it’s place?
It shouldn’t matter as Roblox automatically places those scripts in your character. You can confirm this through the Explorer when debugging.
My issue here is that your script doesn’t seem to have a way of starting? Have you posted the full script?
In the example you provide, the entire code is inside a Touched function, even the event connection. This also wouldn’t work as the script is placed under the Character model, and not a part.
Well the problem must lie within the function itself, it’s not being fired properly. The touched event is inside of the function it’s meant to fire which shouldn’t work.
function onTouched(hit)
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
camera.CameraType = Enum.CameraType.Scriptable
local targetDistance = 30
local cameraDistance = -30
local cameraDirection = Vector3.new(-1,0,0)
local currentTarget = cameraDirection*targetDistance
local currentPosition = cameraDirection*cameraDistance
game:GetService("RunService").RenderStepped:connect(function()
local character = player.Character
if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
local torso = character.HumanoidRootPart
camera.Focus = torso.CFrame
if torso:FindFirstChild("FastStart") == nil then
camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y + 10, torso.Position.Z - 20) + currentPosition,
Vector3.new(torso.Position.X, torso.Position.Y, torso.Position.Z - 20) + currentTarget)
else
camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentPosition,
Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentTarget)
end
end
end)
end
script.Parent.Touched:connect(onTouched)
To reset the camera, put the following code at the top of your script.
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Custom --Reset camera type
cam.CameraSubject = script.Parent:WaitForChild('Humanoid') --Reset camera subject
Since your script is in starter character scripts, this snippet will fix the camera every time the character respawns.
When you die, it still goes back to the camera angle. Here, let me try and explain better, lol. So basically I;m trying to achieve something when the player dies it does this:
When the camera angle changes, and the player dies, it goes back to the normal roblox camera, you know what I mean?Hopefully the GIF explains. Your version does this:
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local IsAtAnAngle = false
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = script.Parent:WaitForChild('Humanoid')
camera.CameraType = Enum.CameraType.Scriptable
script.Parent.PrimaryPart.Touched:Connect(function(hit)
--IF HIT == THE BRICK THAT ACTIVATES THE CAMERA THEN
IsAtAnAngle = true
end)
game:GetService("RunService").RenderStepped:connect(function()
if IsAtAnAngle == true then
local targetDistance = 30
local cameraDistance = -30
local cameraDirection = Vector3.new(-1,0,0)
local currentTarget = cameraDirection*targetDistance
local currentPosition = cameraDirection*cameraDistance
local character = player.Character
if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
local torso = character.HumanoidRootPart
camera.Focus = torso.CFrame
if torso:FindFirstChild("FastStart") == nil then
camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y + 10, torso.Position.Z - 20) + currentPosition,
Vector3.new(torso.Position.X, torso.Position.Y, torso.Position.Z - 20) + currentTarget)
else
camera.CoordinateFrame = CFrame.new(Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentPosition,
Vector3.new(torso.Position.X, torso.Position.Y - 15, torso.Position.Z - 20) + currentTarget)
end
end
end
end)
Make sure to add the condition highlighted in the script sent
So if say you have a brick that activates the camera side view, you will want to check for it:
local ActivatePart = game.Workspace.ActivatePart
script.Parent.PrimaryPart.Touched:Connect(function(hit)
if hit == ActivatePart then
IsAtAnAngle = true
end
end)