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.
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
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)
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