There are a bunch of solutions to this, here's my take on it.
There is a couple things to change. Firstly, try not to use wait()in your scripts, however if you must, try outtask.wait()`.
Anyway, to replace it, we use TweenService, it makes the transition pretty smooth, and is a better alternative to wait() here.
Then we create a function which contains the majority of the code, and call it at the end to start everything off. We will call the function with a parameter later used to determine which part we should listen to .Touched.
EDIT [1]: listenTo is a parameter, it can technically be any names. It's somewhat like a placeholder, and we set listenTo's value to whatever we pass along with the function call.
changeConnection is the function we defined, it's like a storage for code, it does not execute until we call it with changeConnection(any_parameter_we_want, it_can_be_multiple_as_well). So whenever we do changeConnection('Outside'), the code inside of the function runs, and the variable we used in there, aka listenTo's value, will be 'Outside'. If we instead did changeConnection('Inside'), listenTo's value will be 'Inside'.
If we want to listen for .Touched on the house's floor, we call the function with ambientChange('Inside'), vice versa.
Now, for example we called it with 'Outside', the function will connect ambientChange to a .Touched event for the outdoorFloor, so whenever a player steps out of the house, and onto outside's floor, this event will trigger.
Inside the event, we will gradually increase the rain sound and decrease the music with tween, afterwards we stop listening to outdoorFloor and listen to houseFloor instead.
This is probably not the best explanation/method out there, but it should work.
local TweenService = game:GetService('TweenService')
local houseFloor = 'path to house\'s floor'
local outdoorFloor = 'path to outdoor floor'
local rainSound = 'path to rain sound'
local musicSound = 'path to music sound'
rainSound:Play()
musicSound:Play()
local ambientChange
local function changeConnection(listenTo)
if listenTo == 'Inside' then
ambientChange = houseFloor.Touched:Connect(function()
TweenService:Create(rainSound, TweenInfo.new(), {Volume = 0}):Play()
TweenService:Create(musicSound, TweenInfo.new(), {Volume = 1}):Play()
ambientchange:Disconnect()
changeConnection('Outside')
end)
else
ambientChange = outdoorFloor.Touched:Connect(function()
TweenService:Create(rainSound, TweenInfo.new(), {Volume = 1}):Play()
TweenService:Create(musicSound, TweenInfo.new(), {Volume = 0}):Play()
ambientchange:Disconnect()
changeConnection('Inside')
end)
end
end
changeConnection('Outside')
1 comment
Hi there, does my answer provide a valid solution?