How to reset this script when a player dies?

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
2 Likes

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).

2 Likes

It already is in StarterCharacterScripts.

1 Like

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.

So the camera goes back to the normal camera position? If you know what I mean.

image

The script is in SCS already. I do have a startercharacter though, does that mater?

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.

yes it stays in its place, i would like it to reset to the normal camera, and no, there isnt any errors.

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)

This is what it should be, test this

yes this is the fill script, let me send a gyazo if the helps.

https://gyazo.com/eb049e815975174ba4c4d42b7f76ffc3

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.

1 Like

@REALTimothy0812 These don’t work. If you need a place file, you can test it out here:

yes2.rbxl (51.6 KB)

It’s hard to explain, and I’m not the best at explaining, so thats the place file lmao.

Just an update,

script.Parent.Touched:connect(onTouched)

A model doesn’t have a touched function

script.Parent.PrimaryPart.Touched:connect(onTouched)
1 Like

The script doesn’t seem to work at all.

Are you sure the issue is the script not resetting?

I’d remove the Touched function and just keep the content inside of it if I were you. What is the script meant to do though?

1 Like

the script is meant to change the cameras position, I don’t know why It isn’t working? Odd.

yes2.rbxl (51.7 KB)

@ScytheSlayin
Here it is, It works now, i removed the touched function and just put it on the loop

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:

https://gyazo.com/4ed6585ff751897652a83a471a64ee3d

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:

https://gyazo.com/02063c8c6f7e481a0baec9bff5a09e9d

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

1 Like

Dont understand this part, do I insert a touched thing here? Sorry, I’m still new to all of this :thinking:

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)

Sorry for the slow reply!