How to play audio if player is in distance?

i have code that shows a gui if a player is near a block but i want it if the player is on that block to play audio

local Players = game:GetService("Players")
local RS = game:GetService("RunService")

--//Player Components
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

--//Components
local ShopPosition = Workspace.VisualAssets.Assets.Shop.ShopTouch.Position
local ShopGui = PlayerGui:WaitForChild("Shop")
local debounce = true

--Init Function
RS.RenderStepped:connect(function()
	local Distance = Player:DistanceFromCharacter(ShopPosition)
	if (Distance <= 6 and debounce) then
		debounce = false 
		eg:Play()
		ShopGui.MainFrame:TweenPosition(UDim2.new(0.288, 0,0.241, 0), "Out", "Back", 1, true)
		wait(1)
		debounce = true
	elseif(Distance > 6 and debounce)then
		debounce = false
		ShopGui.MainFrame:TweenPosition(UDim2.new(0, -1000,0.241, 0), "Out", "Back", 1, true)
		wait(2)
		debounce = true
	end
end)

If you want a part to play music when touched, try using this method.

Im using playerdistance i want it also if the player leaves it also plays

Can’t you just put eg:Play() in the second part of the script too? It seems like you already figured out how to make the GUI disappear based on distance, I don’t see why you can’t do the same with sound.

because the audio would keep repeating

because its using RS.RenderStepped:connect(function()

From what I got, you were saying that you want the sound to play when the player leaves the area. If you want it to stop, try eg:Stop() instead.

The audio would keep playing let me get a example

Have you tried using Region3 methods?

local RS = game:GetService("RunService")

--//Player Components
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

--//Components
local ShopPosition = Workspace.VisualAssets.Assets.Shop.ShopTouch.Position
local ShopGui = PlayerGui:WaitForChild("Shop")
local debounce = true

--Init Function
RS.RenderStepped:connect(function()
	local Distance = Player:DistanceFromCharacter(ShopPosition)
	if (Distance <= 6 and debounce) then
		debounce = false 
		PlayerGui.ShopKeeper.Welcome:Play()
		ShopGui.MainFrame:TweenPosition(UDim2.new(0.288, 0,0.241, 0), "Out", "Back", 1, true)
		wait(1)
		debounce = true
	elseif(Distance > 6 and debounce)then
		debounce = false
		ShopGui.MainFrame:TweenPosition(UDim2.new(0, -1000,0.241, 0), "Out", "Back", 1, true)
		wait(2)
		debounce = true
	end
end)
``` if i step it keeps playing

well never used region3 xd :sob:

Well, try it, let me get you a code reference.

1 Like

u can try putting the audio under the part and then try adjusting with the audio’s min and max play distance in the properties and set the audio to linear so it’ll fade in whenever the player goes near to the part

Try this. Create a part that would be playing music if a player is having any of it’s limbs inside it.

local Max = game.Workspace.Part.Position + game.Workspace.Part.Size/2
local Min = game.Workspace.Part.Position - game.Workspace.Part.Size/2
local Region = Region3.new(Min,Max)

local RS = game:GetService("RunService")

--//Player Components
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui

--//Components
local ShopPosition = workspace.VisualAssets.Assets.Shop.ShopTouch.Position
local ShopGui = PlayerGui:WaitForChild("Shop")
local debounce = true

--//Init Function
function CanPlayerHearMusic(player)
	local Response = false
	if workspace:FindPartsInRegion3WithWhiteList(Region,player.Character) ~= nil then
		Response = true
	end
	return Response
end

RS.RenderStepped:connect(function()
	local Distance = Player:DistanceFromCharacter(ShopPosition)
	if CanPlayerHearMusic(Player) then
		debounce = false 
		PlayerGui.ShopKeeper.Welcome:Play()
		ShopGui.MainFrame:TweenPosition(UDim2.new(0.288, 0,0.241, 0), "Out", "Back", 1, true)
		wait(1)
		debounce = true
	else
		debounce = false
		ShopGui.MainFrame:TweenPosition(UDim2.new(0, -1000,0.241, 0), "Out", "Back", 1, true)
		wait(2)
		debounce = true
	end
end)

19:23:05.021 Unable to cast value to Objects - i get this lol

Which line does it direct you to?

Line 19 if workspace:FindPartsInRegion3WithWhiteList(Region,player.Character) ~= nil then

if workspace:FindPartsInRegion3WithWhiteList(Region,player.Character) then

Try that instead.

If I’m not mistaken, sounds have a property named Playing. Instead of playing the sound through :Play() (which causes the sound to restart), you could change Playing based on the distance.

Like this, for example:

PlayerGui.ShopKeeper.Welcome.Playing = Distance <= 6 --will be either true or false