Function only "applies" once, wont work after respawning!

local function ApplyTransparency(Character)	
	for _, Piece in ipairs(Character:GetDescendants()) do
		if Piece:IsA("BasePart") and Piece.Name ~= "Head" then
			if Piece.Parent:IsA("Accessory") then
				if Piece.Parent.AccessoryType == Enum.AccessoryType.Face or Piece.Parent.AccessoryType == Enum.AccessoryType.Hair or Piece.Parent.AccessoryType == Enum.AccessoryType.Hat then
					continue 
				end
			end

			Piece:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
				Piece.LocalTransparencyModifier = Piece.Transparency
			end)

			Piece.LocalTransparencyModifier = Piece.Transparency
		end
	end
end

Right now this is a function I have,

function CameraModule:FirstPerson(Camera, Character, Client)
	if not Character then
		return
	end
	
	local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
		Camera.CFrame = Camera.CFrame * shakeCf
	end)

	camShake:Start()

	-- Explosion shake:
	camShake:ShakeSustain(CameraShaker.Presets.HandheldCamera)

	
	local CameraDirection = Vector2.new()
	
	Client.CameraMaxZoomDistance = 0
	
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	
	ApplyTransparency(Character)
end

As well the function that calls it, and my biggest issue is that it will work once when the character loads in/spawns in for the first time, but refuses to work after resetting, any help?

1 Like

Are you making sure to rereference the character when the player dies

1 Like

Yes, those both functions are actually inside a module, in which the CameraModule:FirstPerson, gets called inside a script inside “StarterCharacterScripts”

1 Like

To add onto this, I’ve tried to reference a new character, but it still creates the same issue and it is frustrating by this point.

1 Like

Can you show that script please?

Local script, inside StarterCharacterScripts

local Client = game:GetService("Players").LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")

local CameraModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Client.CameraModule)

local Camera = game.Workspace.CurrentCamera

CameraModule:FirstPerson(Camera, Character,Client)

CameraModule

function CameraModule:FirstPerson(Camera, Character, Client)
	if not Character then
		return
	end
	
	local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
		Camera.CFrame = Camera.CFrame * shakeCf
	end)

	camShake:Start()

	-- Explosion shake:
	camShake:ShakeSustain(CameraShaker.Presets.HandheldCamera)

	
	local CameraDirection = Vector2.new()
	
	Client.CameraMaxZoomDistance = 0
	
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	
	for _, Piece in pairs(Character:GetDescendants()) do
		if Piece:IsA("BasePart") and Piece.Name ~= "Head" then
			if Piece.Parent:IsA("Accessory") then
				if Piece.Parent.AccessoryType == Enum.AccessoryType.Face or Piece.Parent.AccessoryType == Enum.AccessoryType.Hair or Piece.Parent.AccessoryType == Enum.AccessoryType.Hat then
					continue 
				end
			end

			Piece:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
				Piece.LocalTransparencyModifier = Piece.Transparency
			end)

			Piece.LocalTransparencyModifier = Piece.Transparency
		end
	end
	
	local UpperTorso = Character:WaitForChild("UpperTorso")
	
	RunService.RenderStepped:Connect(function(Delta)
	end)
end

I am now using a slightly older version (js doesnt use a function anymore)

I do realize now it might be an issue with a new character not being referenced, but the issue still maintains even after all my debugs and its so annoying.

Try using script.Parent as the reference to the character.

Issue persists, arguably worse since it causes more errors.

I meant in the script in StarterCharacter.

mhm, i did that, it still persists

Is the function running after respawning at all?

If i add a print to the module function, the print will, print. but the function stops working which goes back to my point that it might have something to do with the character.

Can you show the other functions in the module please?

None of the other functions contribute to the issue as they operate without the character

How do you know it’s the character? I can’t really help you if I can’t see everything necessary.

Because I’ve narrowed it down (at least I hope I did), I am not going to show unnecessary functions that do not have any correlation with the main issue/function.

Okay so what part isn’t working exactly?

If you applying CameraModule:FirstPerson(Camera, Character, Client) right after humanoid dies like:

Maybe this is your error:

Humanoid.Died:Connect(function()
      CameraModule:FirstPerson(Camera, Character, Client)
end)

Why is this happening?

  • After Humanoid Dies that function applies on old character because new character is not spawned(In short this applies only to old character)

If this is error you should use

Humanoid.Died:Connect(function()
      task.wait(game.Players.RespawnTime + 0.5) -- you can adjust + 0.5 as you need(although that makes little delay, that is important to make )
      CameraModule:FirstPerson(Camera, Character, Client)
end)

if this is you full local script then problem is when player dies, roblox’s system creates new character and removes old character, same for camera it reserts.

this should be your local script:

local Client = game:GetService("Players").LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local UserInputService = game:GetService("UserInputService")

local CameraModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Client.CameraModule)

local Camera = game.Workspace.CurrentCamera

CameraModule:FirstPerson(Camera, Character,Client)

Note that local script should be inside of StarterCharacterScript
or you can use other method that doesn’t require you to place in StarterCharacterScript:

Don’t change variables, if you change script would not work:

  1. NewCamera
  2. NewCharacter
local Client = game:GetService("Players").LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")

local CameraModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Client.CameraModule)

local Camera = game.Workspace.CurrentCamera

CameraModule:FirstPerson(Camera, Character,Client)

Client.CharacterAdded:Connect(function(NewCharacter)
	task.wait(game.Players.RespawnTime + 0.5) -- you can adjust + 0.5 as you need(although that makes little delay, that is important to make )
	local NewCamera = workspace.CurrentCamera
	CameraModule:FirstPerson(NewCamera, NewCharacter, Client)
end)

:frowning_with_open_mouth:

Line 61

for _, Piece in pairs(Character:GetDescendants()) do
		if Piece:IsA("BasePart") and Piece.Name ~= "Head" then
			if Piece.Parent:IsA("Accessory") then
				if Piece.Parent.AccessoryType == Enum.AccessoryType.Face or Piece.Parent.AccessoryType == Enum.AccessoryType.Hair or Piece.Parent.AccessoryType == Enum.AccessoryType.Hat then
					continue 
				end
			end

			Piece:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
				Piece.LocalTransparencyModifier = Piece.Transparency
			end)

			Piece.LocalTransparencyModifier = Piece.Transparency
		end
	end

Line 79

RunService.RenderStepped:Connect(function(Delta)
		
		Camera.CameraType = Enum.CameraType.Scriptable
		
		CameraDirection -= UserInputService:GetMouseDelta() * DefaultCameraValues.CurrentSensitivity
		CameraDirection = Vector2.new(CameraDirection.X % 360, math.clamp(CameraDirection.Y, -85, 85))

		local Value = CFrame.new(UpperTorso.Position) * CFrame.new(0, 1.5, 0) * CFrame.fromOrientation(math.rad(CameraDirection.Y), math.rad(CameraDirection.X), 0) 
			* CFrame.new(0,0.4 + (-CameraDirection.Y/90*0.2),(-CameraDirection.Y/90*0.2))
		
		Value = Value + Character.UpperTorso.CFrame.LookVector * 1.05
		
		Camera.CFrame = Camera.CFrame:Lerp(Value, 0.085)
	end)