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