i want event to fire every time player dies
but if you die event stops firing at all
same thing with Humanoid.Died function
i tried searching dev forum posts and other stuff
Ai:
wait(game.Loaded)
local PathfindingService = game:GetService("PathfindingService")
local Scrap = script.Parent
local humanoid = Scrap:WaitForChild("Humanoid")
local Humanoid_Root_Part = Scrap:WaitForChild("HumanoidRootPart")
local Door1 = workspace.Map.Office.WorkingParts.Door1
local Door2 = workspace.Map.Office.WorkingParts.Door2
local Scrap_Death_Event = game:GetService("ReplicatedStorage").Remotes.Scrap_Death_Event
Humanoid_Root_Part:SetNetworkOwner(nil)
local pathParams = {
AgentHeight = 5,
AgentRadius = 3,
AgentCanJump = true,
}
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {Scrap}
local lastPos
local RANGE = 150
local DAMAGE = 100
local function canSeeTarget(target)
local origin = Humanoid_Root_Part.Position
local direction = (target.HumanoidRootPart.Position - Humanoid_Root_Part.Position).Unit * RANGE
local ray = workspace:Raycast(origin, direction, rayParams)
if ray then
if ray.Instance:IsDescendantOf(target) then
return true
else
return false
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = RANGE
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Humanoid_Root_Part.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
else
end
end
end
return nearestTarget
end
local function getPath(destination)
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Humanoid_Root_Part.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Humanoid_Root_Part.Position - target.HumanoidRootPart.Position).Magnitude
local debounce = false
local player = game.Players:GetPlayerFromCharacter(target)
if distance > 5 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
Scrap_Death_Event:FireClient(player)
Scrap.Head.JumpScare:Play()
task.wait(1.5)
target.Humanoid.Health -= DAMAGE
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
path.Blocked:Connect(function()
path:Destroy()
findTarget()
getPath(destination)
end)
local target = findTarget()
if target and target.Humanoid.Health > 0 then
lastPos = target.HumanoidRootPart.Position
attack(target)
break
else
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
if lastPos then
humanoid:MoveTo(lastPos)
humanoid.MoveToFinished:Wait()
lastPos = nil
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
else
return
end
end
local function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while task.wait() do
patrol()
end
player Died Script:
wait(game.Loaded)
--//Services
local Run_Service = game:GetService("RunService")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Tween_Service = game:GetService("TweenService")
--//Connections
local Scrap_Connection:RBXScriptConnection = nil
local Bobby_Connection:RBXScriptConnection = nil
--//Player
local Player = game.Players.LocalPlayer
local Character = Player.Character
--//Events
local Bobby_Event = Replicated_Storage.Remotes.BobbyDeathEvent
local Scrap_Event = Replicated_Storage.Remotes.Scrap_Death_Event
--//Positions
local Spawning_Room_Position = workspace.SpawningRoom.SpawningBlock.Position
--//Camera
local Current_Camera = workspace.CurrentCamera
--//Jumpscare npcs
local Scrap_Jumpscare = workspace.ScrapJumpScareRoom.JumpScare
local Bobby_Jumpscare = workspace.BobbyJumpScareRoom.JumpScare
--//Gui
local Screen_Gui = script.Parent.Screen
local BackGround = Screen_Gui.BackGround
local Bottom_Text = Screen_Gui.Bottom_Text
local Upper_Text = Screen_Gui.Upper_Text
--//Tables
local Lower_Text_Table = {
"| You never expect to die this way huh? |"
}
------------------------------------------------------
--Main Code
--// Functions
local function Scrap_Death_Effect()
local Scrap_Jumpscare_Camera = Scrap_Jumpscare:WaitForChild("JumpScareCamera")
Character:MoveTo(Spawning_Room_Position + Vector3.new(0, 3, 0))
Current_Camera.CameraType = Enum.CameraType.Scriptable
Current_Camera.CFrame = Scrap_Jumpscare_Camera.CFrame
Current_Camera.FieldOfView = 90
Scrap_Jumpscare.Head.JumpScare:Play()
warn("Please Fire this event")
end
local function Bobby_Death_Effect()
local Bobby_Jumpscare_Camera = Bobby_Jumpscare:WaitForChild("JumpScareCamera")
Character:MoveTo(Spawning_Room_Position + Vector3.new(0, 3, 0))
Current_Camera.CameraType = Enum.CameraType.Scriptable
Current_Camera.CFrame = Bobby_Jumpscare_Camera.CFrame
Current_Camera.FieldOfView = 90
end
Scrap_Connection = Scrap_Event.OnClientEvent:Connect(Scrap_Death_Effect)
Bobby_Connection = Bobby_Event.OnClientEvent:Connect(Bobby_Death_Effect)
local function Player_Died()
print("Player Died")
Bottom_Text.Text = Lower_Text_Table[math.random(1, #Lower_Text_Table)]
BackGround.Transparency = 0
wait(1)
for i, v in pairs(Screen_Gui:GetDescendants()) do
if v:IsA("ImageLabel") then
Tween_Service:Create(v, TweenInfo.new(1), {ImageTransparency = 0}):Play()
elseif v:IsA("TextLabel") then
Tween_Service:Create(v, TweenInfo.new(1), {TextTransparency = 0}):Play()
end
Current_Camera.CameraType = Enum.CameraType.Custom
wait(0.5)
Bobby_Connection:Disconnect()
Scrap_Connection:Disconnect()
end
end
Character.Humanoid.Died:Connect(Player_Died)