Hello, can somebody point out why a function as simple as this is not running? There must be one considering it refuses to run when the character is added/respawns.
function module.deathCam(player : Player)
local char : Model = player.Character or player.CharacterAdded:Wait()
local humanoid : Humanoid = char.Humanoid
local cam = workspace.CurrentCamera
local connection
local RS = game:GetService("RunService")
player.CharacterAdded:Connect(function() --ts pmo icl
print("its working")
if cam.CameraType ~= Enum.CameraType.Custom then
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = player.Character.Humanoid
player.CameraMode = Enum.CameraMode.LockFirstPerson
end
end)
Can you send the script where you are calling this function? I just tested the script and it works fine, other than the snippet you sent missing an ââendââ
Yes itâs just the first half of the function. Here is where I call it:
local module = require(script.module)
local starterGUI = game:GetService("StarterGui")
local cam = workspace.CurrentCamera
local localPLR = game:GetService("Players").LocalPlayer
if localPLR.Character then
script.Parent.Enabled = true
else
script.Parent.Enabled = false
end
module.KillstreakScript() --initialize
module.Timer()
module.deathCam(localPLR)
The second half has to do with the camera subject changing to be the playerâs killer, and that works fine. It is just this .CharacterAdded signal that isnât firing.
I could totally see this being a problem, if it werenât for the fact that the second half of the script, the connection to Humanoid.Died, does work.
Hereâs the full code.
function module.deathCam(player : Player)
local char : Model = player.Character or player.CharacterAdded:Wait()
local humanoid : Humanoid = char.Humanoid
local cam = workspace.CurrentCamera
local connection
local RS = game:GetService("RunService")
game:GetService("Players").LocalPlayer.CharacterAdded:Connect(function() --dumb ahh script don't work in the module don't even ask me the mfs on devforum don't know either
print("its working")
if cam.CameraType ~= Enum.CameraType.Custom then
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = player.Character.Humanoid
player.CameraMode = Enum.CameraMode.LockFirstPerson
end
end)
humanoid.Died:Connect(function()
local endPos = char.HumanoidRootPart.Position + Vector3.new(0, 5, 0)
local creator = humanoid:FindFirstChild("creator")
if creator ~= nil and creator.Value ~= nil then
local deathPos : Vector3 = char.Head.Position
local killer : Player = creator.Value
local killerChar : Model = killer.Character
player.CameraMode = Enum.CameraMode.Classic
cam.CameraType = Enum.CameraType.Watch
cam.CameraSubject = killerChar.Humanoid
end
end)
Please read my first post under your thread.
I explained why it doesnt fire.
You yeild script in 2nd line and connect signal after event already firing
It looks like each time you call module.deathCam it is redefining the Player.
So, the Player.CharacterAdded will never fire since the character already loaded in the script that called the function:
local module = require(script.module)
local starterGUI = game:GetService("StarterGui")
local cam = workspace.CurrentCamera
local localPLR = game:GetService("Players").LocalPlayer
if localPLR.Character then
script.Parent.Enabled = true
else
script.Parent.Enabled = false
end
--module.KillstreakScript() --initialize
--module.Timer()
module.deathCam(localPLR)
The script above states: if localPLR.Character then
After that it states: module.deathCam(localPLR)
So, the character has already loaded before the module is even called.
Yes scripts execute top to bottom, but a script will not stop after it carries out an if __ then statement.
You will notice that after the if__then statement, there is an end. This means lines of code below are unnaffected by the outcome of if localPLR.Character then because the statement ended.
But, I donât understand why you are trying to listen for CharacterAdded when you already have the char?
(That is why the Humanoid.Died works, because you are using the Char you already have.)
So why are you listening for CharacterAdded?
function module.deathCam(player : Player)
local char : Model = player.Character or player.CharacterAdded:Wait()
local humanoid : Humanoid = char.Humanoid
local cam = workspace.CurrentCamera
local connection
local RS = game:GetService("RunService")
game:GetService("Players").LocalPlayer.CharacterAdded:Connect(function() --dumb ahh script don't work in the module don't even ask me the mfs on devforum don't know either
print("its working")
if cam.CameraType ~= Enum.CameraType.Custom then
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = player.Character.Humanoid
player.CameraMode = Enum.CameraMode.LockFirstPerson
end
end)
This is because I need to know when the character respawns, in order to reset their camera back to their player. Unless I am mistaken, this can only be attained via .CharacterAdded
local module = require(script.module)
local starterGUI = game:GetService("StarterGui")
local cam = workspace.CurrentCamera
local localPLR = game:GetService("Players").LocalPlayer
if localPLR.Character then
script.Parent.Enabled = true
else
script.Parent.Enabled = false
end
module.KillstreakScript() --initialize
module.Timer()
localPLR.CharacterAdded:Connect(module.ResetCam)
Here is the module:
function module.ResetCam(char)
local player = game:GetService("Players").LocalPlayer
local humanoid = char.Humanoid
local cam =workspace.CurrentCamera
local connection
if cam.CameraType ~= Enum.CameraType.Custom then
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject =char.Humanoid
player.CameraMode = Enum.CameraMode.LockFirstPerson
end
humanoid.Died:Once(module.deathCam)
end
function module.deathCam()
local player = game:GetService("Players").LocalPlayer
local char = player.Character
local humanoid = char.Humanoid
local cam = workspace.CurrentCamera
local endPos = char.HumanoidRootPart.Position + Vector3.new(0, 5, 0)
local creator = humanoid:FindFirstChild("creator")
if creator ~= nil and creator.Value ~= nil then
local deathPos : Vector3 = char.Head.Position
local killer : Player = creator.Value
local killerChar : Model = killer.Character
player.CameraMode = Enum.CameraMode.Classic
cam.CameraType = Enum.CameraType.Watch
cam.CameraSubject = killerChar.Humanoid
end
end
Also, you should define the player as a variable at the outermost scope, at the top of the script; basically as a global variable, instead of defining it again within those functions.
If the CharacterAdded event is not firing, it may be because the character has already been loaded. But to test this, just run some basic function to test if code runs when it reaches the line?
If it runs, then at least youâve isolated your problem to the module script. Keep using these print statements to find where itâs not working as expected
Yes, the .CharacterAdded signal fires now, but not the Humanoid.Died. This should work because each time the character is added, it uses :once to connect to the death can, which does not fire upon death.