Camera change on death

So, I need a little help with a death system. I have a script, that should change the camera to the partcam1 upon death of the character:

local player = game.Players.LocalPlayer

local camera = game.Workspace.CurrentCamera

local part = game.Workspace.partcam1

local sound = game.Workspace.jumscare

function onPlayerDied()

camera.CameraType = Enum.CameraType.Scriptable

camera.CFrame = part.CFrame

sound:Play()

wait(5)

camera.CameraType = Enum.CameraType.Custom

camera.CameraSubject = player.Character.Humanoid

end

player.Character.Humanoid.Died:Connect(onPlayerDied)

however this doesnt seem to work. I dont know much in scripting, so what do I fix here?

Developer Console doesnt help much, as it says there are no errors

If that is the full script then you never actually call the function.

You likely want to connect it to the Humanoid.Died event so that it runs when the humanoid’s health reaches 0.

I didn’t see the last line, sorry.

Seeing that you have connected the event you should check that the script is running at all.

Local scripts only run in one of 4 places:

  • Under the player character
  • In PlayerScripts
  • In the backpack
  • In PlayerGui
2 Likes

Try this:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local part = game.Workspace.partcam1

local function onPlayerDied()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part.CFrame
	
	task.wait(1)
	
	camera.CameraType = Enum.CameraType.Custom
	camera.CameraSubject = humanoid
end

humanoid.Died:Connect(onPlayerDied)

1 Like

what is the difference between function and local function

local camera = game.Workspace.CurrentCamera

local part = game.Workspace.partcam1

local sound = game.Workspace.jumscare

function onPlayerDied()
   repeat wait()
      camera.CameraType = Enum.CameraType.Scriptable
   until camera.CameraType == Enum.CameraType.Scriptable
   camera.CFrame = part.CFrame
   task.delay(5, function() --> Don't interrupt anything else.
      repeat wait()
         camera.CameraType = Enum.CameraType.Custom
      until camera.CameraType == Enum.CameraType.Custom
      camera.CameraSubject = player.Character.Humanoid
   end)
end

player.Character.Humanoid.Died:Connect(onPlayerDied)
1 Like
task.delay(5, function() --> Don't interrupt anything else.```

what does this line of code do?
the code works, however the camera never returns to player

task.dealy(number,function()) simply creates a delay of the given seconds and then calls the provided function

1 Like

The scope is the difference. Local Functions can be used only in the scope they were created, while (global) functions can be used outside their scope.

A crude example to show it vividly

if humanoid.Health > 0 then
   local function changeCamera(subject)
          camera.CameraSubject = subject
   end

   changeCamera(deathCamera) --Will Work
   task.wait(5)
   changeCamera(player.Character.Humanoid) --Will work
end

changeCamera(deathCamera) --Will Error. The function doesn't exist here.

In the above example, the Local Function is created inside the “If” statement scope, so it can only be called inside that scope. If you try and call the function outside of that “If”, it’s gonna throw an error, because the function doesn’t exist outside that scope.

If you remove the “local” in the function above, you can call it anywhere in the script, including outside the “If” scope, and inside other scopes.

There are other minor things, like global function can be called before it is created, like before the line the actual function is written. But the main difference is still the scope.

It is recommended to always use Local for performance benefits, unless you Have To use global.

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