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

How do I detect if another script is playing?

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

1 comment

0

Hi there, does my answer provide a valid solution?

1 answer

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'

--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')

11 comments

0

I attempted to use this script, the Tween works however the music does not play. I would also love it if you could tell me what listenTo and changeConnection does, as I am a beginner scripter and I do not know many commands

0

Ah, I probably forgot to actually call the :Play() to the sound. I'll edit the post to add explanation and a quick fix to no sound.

0

It's a very laggy script and the rain doesn't play... should I add a wait() command?

0

I don’t think a wait() is needed. Can you elaborate on how “very laggy” the script is? Also, have you checked the properties of the rain sound?

0

Yes, both are volume 0. By laggy, I mean it's as if it keeps playing the music sound, like it's spamming it every step i walk. So, when I stop moving the music plays like normal, but when I walk it spams play because i'm touching the floor, which causes to drop my FPS

0

Try adding a :Disconnect(), perhaps that would solve the issue. I have edited the post accordingly.

0

It still doesn't work, what it does is the rain is still playing but the music doesn't do anything at all. I tried with both Playing set to off and set to on and I also tried both volume 1. It doesn't work

0

Okay, let me confirm the situation: The rain plays expectedly, the laggy issue is solved, and the issue right now is that the music inside the house does not play?

0

Just some generic suggestions at this point: Check if the audios are uploaded by yourself, Looped is checked in both sound’s property, and are there any errors?

0

No, they arent uploaded by myself, looped is checked and no last time I checked there wasnt any

0

Okay, this is just an assumption, but I think it is because the uploader is not you, Roblox recently pushed an update regarding audios. Try testing it with an audio uploaded by yourself.

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