Hi there, I’m trying to make it so that when the player dies, it exits them out of this FPS mode. But it doesn’t work.
FPS body script:
-- initialize local variables
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
-- camera settings
player.CameraMaxZoomDistance = 0.5 -- force first person
camera.FieldOfView = 100
humanoid.CameraOffset = Vector3.new(0, 0, -1)
-- set and keep every body part Transparency to its real transparency
for childIndex, child in pairs(character:GetChildren()) do
if child:IsA("BasePart") and child.Name ~= "Head" then
child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
child.LocalTransparencyModifier = child.Transparency
end)
child.LocalTransparencyModifier = child.Transparency
end
end
-- if the player steps in a vehicle
camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
if camera.CameraSubject:IsA("VehicleSeat") then
camera.CameraSubject = humanoid
end
end)
You only disable the script, the event connections are still made.
You probably need to go through the connected events, which means putting them into a table and disconnecting them on death
Something like this
-- initialize local variables
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
-- camera settings
player.CameraMaxZoomDistance = 0.5 -- force first person
camera.FieldOfView = 100
humanoid.CameraOffset = Vector3.new(0, 0, -1)
local events = {}
-- set and keep every body part Transparency to its real transparency
for childIndex, child in pairs(character:GetChildren()) do
if child:IsA("BasePart") and child.Name ~= "Head" then
table.insert(events,child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
child.LocalTransparencyModifier = child.Transparency
end))
child.LocalTransparencyModifier = child.Transparency
end
end
-- if the player steps in a vehicle
camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
if camera.CameraSubject:IsA("VehicleSeat") then
camera.CameraSubject = humanoid
end
end)
humanoid.Died:Connect(function()
for _, event in pairs(events) do
event:Disconnect()
end
end)
No need for checks since we know the table will only contain events.
If this is in somewhere where it wont repeat on respawn, you’ll have to include code to make the events be created again
-- initialize local variables
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
-- camera settings
player.CameraMaxZoomDistance = 0.5 -- force first person
camera.FieldOfView = 100
humanoid.CameraOffset = Vector3.new(0, 0, -1)
local events = {}
local function setTransparency(transparency)
for childIndex, child in pairs(character:GetChildren()) do
if child:IsA("BasePart") and child.Name ~= "Head" then
if not transparency then
table.insert(events,child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
child.LocalTransparencyModifier = child.Transparency
end))
end
child.LocalTransparencyModifier = transparency or child.Transparency
end
end
end
-- set and keep every body part Transparency to its real transparency
setTransparency()
-- if the player steps in a vehicle
camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
if camera.CameraSubject:IsA("VehicleSeat") then
camera.CameraSubject = humanoid
end
end)
humanoid.Died:Connect(function()
for _, event in pairs(events) do
event:Disconnect()
end
setTransparency(1)
end)