The closer a player is to an NPC, the faster the heartbeat becomes and the redder the screen becomes

I have read several articles online about this, but none have satisfied me. my goal would be to have a script inside the monster (which is played by a player), which makes it so that the closer it gets to a player, the more the player hears the heartbeat sound, which is found in the monster’s HumanoidRootPart , fast. I would also like that in the same way as the heartbeat, the closer the monster gets to a player, the more the player’s screen becomes red and, if possible, shakes.
Any help is appreciated! :slight_smile:

2 Likes

I think those functions should be handled on the client only. Place all the NPCs into a dedicated folder in workspace, then have a loop on the client that checks the Magnitude from players character to the NPC. If it is below certain values, play the sound. To achieve the screen colour change, have a UI element that the transparency increases from 1 up as that Magnitude value decreases.

And how would i do that? sorry i’m not a very good scripter haha

The check is similar to that used for NPCs hunting for players:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local playerHRP = character:WaitForChild("HumanoidRootPart")

local heartbeatSound = script:WaitForChild("HeartBeatSound")  -- Location of Heartbeat sound
heartbeatSound:Play() -- start sound
local redGui = player:WaitForChild("PlayerGui"):WaitForChild("RedGui"):WaitForChild("RedFrame"):WaitForChild("img_Border")

local NPCFolder = workspace:WaitForChild("NPCs") -- Folder to searcg for NPCs
local maxSearchDist = 100 -- Maximum distance to search for NPCs

-- GAME LOOP
while true do  -- continually check for NPC distance
	task.wait(0.1)
-- Find distance to closest NPC	
	local nearestNPCDist = maxSearchDist
	for _, NPC in ipairs(NPCFolder:GetChildren()) do  -- loop thru every NPC in the folder
		if NPC.Humanoid and NPC.Humanoid.Health > 0 then  --check if its alive
			local targetHRP = NPC:FindFirstChild("HumanoidRootPart")
			if not targetHRP then return end  -- sanity check for HRP
			local distance = (playerHRP.Position - targetHRP.Position).Magnitude -- check distance to NPC
			if distance < nearestNPCDist then -- if NPC is the closest
				nearestNPCDist = distance -- set this as nearest distance for comparison
			end
		end
	end
-- Set Heartbeat Sound and Red Frame
	local redTransparency = 0.01 * nearestNPCDist
	redGui.ImageTransparency = redTransparency
	local heartbeatSpeed = 2 - (0.01 * nearestNPCDist)
	heartbeatSound.PlaybackSpeed = heartbeatSpeed
end

Heartbeat.rbxl (85.0 KB)

I’ve added a sample place file that has a nice image border and the heartbeat sound.

  • local script under StarterPlayer > StarterCharacterScripts
  • Heart beat sound under the local script
  • RedGui with frame and a fancy image border

it works good, any idea on the screen shake?