I already know how, but it seems to not work.
My goal here is that when you touch the floor of inside the house, music starts playing and the rain's volume gets weaker. When you go outside, the music stops and the rain's volume becomes stronger.
Here are my scripts:
Script 1
local touchpart = workspace.Floor local outside = workspace.OUTTOUCH local rain = workspace.Rain local sound = script.Parent local boolvalue = script.Parent.Value if boolvalue.Value == false then touchpart.Touched:Connect(function() boolvalue.Value = true rain.Volume = 0.3 wait(0.1) rain.Volume = 0.2 wait(0.1) rain.Volume = 0.1 touchpart.CanTouch = false sound.Volume = 2 sound:Play() end) outside.CanTouch = true end
Script 2
local touchpart = workspace.OUTTOUCH local inside = workspace.Floor local rain = workspace.Rain local sound = script.Parent local boolvalue = workspace.Sound.Value if boolvalue.Value == true then touchpart.Touched:Connect(function() boolvalue.Value = false inside.CanTouch = false rain.Volume = 0.2 wait(0.1) rain.Volume = 0.3 wait(0.1) rain.Volume = 0.4 wait(0.1) rain.Volume = 0.5 sound:Stop() end) touchpart.CanTouch = true end
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 out
task.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' --START OF EDIT [1]-- --remember to set both sound's volume to 0 in the properties panel rainSound:Play() musicSound:Play() --END OF EDIT [1]-- 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() --START OF EDIT [2]-- ambientchange:Disconnect() --END OF EDIT [2]-- 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() --START OF EDIT [2]-- ambientchange:Disconnect() --END OF EDIT [2]-- changeConnection('Inside') end) end end changeConnection('Outside')