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 don’t think CharacterAdded will work inside a Module.
- consider moving RS outside of function
- For auto completion of services i recomend using but that more of a “pro tip” there is many others
- You obviously wont get get becouse you yeild for an event and there for skipping it in your 2nd line of code
Please dont spread misinformation to a people.
Is it possible that module.Timer()
contains a loop, or yields infinitely? Maybe module.deathCam(localPLR)
is never being reached.
Try changing your script to include printing statements to debug it.
print("Initializing!")
module.KillstreakScript() --initialize
print("Reached timer!")
module.Timer()
print("Reached deathcam!")
module.deathCam(localPLR)
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.
Bro…
if localPLR.Character then
end
module.deathCam() -- outside of if then statement
Scripts execute from top to bottom.
I can’t tell if you’re trolling.
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.
Nevermind. I see what you are saying.
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
I see, it seems like maybe this will work for you:
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
localPLR.CharacterRemoving:Connect(function()
localPLR.CharacterAdded:Connect(function()
module.deathCam(localPLR)
end)
end)
module.KillstreakScript() --initialize
module.Timer()
module.deathCam(localPLR)
This updated version has the same results.
Here is where the function is called:
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
So the issue is that deathCam isn’t running? Or the other function too?
what is script.Parent?
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?
For example,
player.CharacterAdded:Connect(function()
print("hello")
end)
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.
This post was solved in a previous reply by LastingSunset