Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
yoshi8080 445 Moderation Voter
8 years ago

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.

0
is the sound a folder or an actual Sound TheDeadlyPanther 2460 — 8y
0
in a folder for where the sound is located yoshi8080 445 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

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!!!

0
Problem: Sound stops and removes after .5 which doesn't run with the distance in the script. yoshi8080 445 — 8y
0
Might as well just remove the whole walk away script. Then, implement a way so you can remove it via Dialog. alphawolvess 1784 — 8y
Ad

Answer this question