Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to remove audio when player walks away from dialog range?

I wanted the sound to remove when the player isn't in the dialog range?

sound = script.Parent.Parent.Parent.Parent.Sound.Song
clonesong = sound:clone()
script.Parent.DialogChoiceSelected:connect(function(player, choice)
	if (choice == script.Parent.Choose.Accept) then
clonesong.Parent = player.PlayerGui
print("Song Played")
clonesong:Play()
	if (choice == script.Parent.Choose.Accept.Info.PriceDetail) then
if player.PlayerGui:FindFirstChild("Song") then
player.PlayerGui:FindFirstChild("Song"):remove()
print("Song Stopped")
		end
	end
	end
end)

No errors in output. I don't know how to remove the sound from the playergui if the player walks away from the dialog range, and the sound won't stop when the certain choice was clicked.

2 comments

0

is the sound a folder or an actual Sound

0

in a folder for where the sound is located

1 answer

Okay, here we go...

First, in your parameters for line 3... player is the person interacting with the Dialog while choice is the selected DialogChoice.

Now, bellow will be the script.

sound = script.Parent.Parent.Parent.Parent.Sound.Song
SoundClone = sound:clone() -- The purpose here is an easy reference on the new song! Allowing us to easily player it later.

script.Parent.DialogChoiceSelected:connect(function(player, choice)
	if choice.Name == "accept" then
		SoundClone.Parent = player.PlayerGui
		SoundClone:Play()
	end
end)

You should understand the Variables, also I have tried to make this compatible with what you have made, however I am unsure of the names of everything, etc.

The first if will check the choice the player has selected, if the DialogChoice is name accept , then you will put the cloned song inside of the PlayerGui and player the song. We need nothing for your decline event because nothing would really happen.

Now, for the Walking away part. I made a variable of the DistanceFromCharacter which will return the distance the player is from the InvisibleField. If the distance is 10 studs, the song is stopped and destroyed. Loop bellow for the script for distance

local player = game.Players.LocalPlayer -- Local Scripts allow instant player find using LocalPlayer
local Distance = player:DistanceFromCharacter(game.Workspace.Stand.InvisibleField.Position)
while wait(.5) do
	if Distance >= 10 then			
		if player.PlayerGui:FindFirstChild("song") then
			player.PlayerGui.song:Pause()
			player.PlayerGui.song:Destroy()
		end
	end
end

Make sure the above script is inside of a LOCALSCRIPT that is the child of StarterPack!!!

2 comments

0

Problem: Sound stops and removes after .5 which doesn't run with the distance in the script.

0

Might as well just remove the whole walk away script. Then, implement a way so you can remove it via Dialog.

This discussion is preserved as it was. New answers, comments, and votes are closed.