How I want it to work is that when a player is a certain distance to a part a heartbeat sound will play but when they walk closer to it the faster the heartbeat will play.
When they get far enough away from the part the heartbeat will stop until the player enters the parts radius again.
Problem is that I don’t really know how to code this.
local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local SoundService = Game:GetService("SoundService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Part = Workspace:WaitForChild("Part") --Part.
local Sound = SoundService:WaitForChild("Sound") --Heartbeat sound.
local MinimumDistance = 20 --Minimum distance for heartbeat sound to be played.
local function OnRenderStep()
if Sound.IsPlaying then return end --Do not play the sound if it is already playing.
local Distance = (Character:GetPivot().Position - Part.Position).Magnitude --Get distance between the player's character and the part.
if Distance > MinimumDistance then return end --Do not play the sound if the distance is too large.
Sound.PlaybackSpeed = 2 - (Distance / MinimumDistance) --Normal speed at distance of 20, double speed at distance of 0.
end
RunService.RenderStepped:Connect(OnRenderStep)
You need an equation that produces smaller values as it is provided larger values.