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

How do I detect if another script is playing?

Asked by 2 years ago

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
0
Hi there, does my answer provide a valid solution? SuperLittleAdmin 257 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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 usewait()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')
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 EricAndTheWell 11 — 2y
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. SuperLittleAdmin 257 — 2y
0
It's a very laggy script and the rain doesn't play... should I add a wait() command? EricAndTheWell 11 — 2y
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? SuperLittleAdmin 257 — 2y
View all comments (7 more)
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 EricAndTheWell 11 — 2y
0
Try adding a :Disconnect(), perhaps that would solve the issue. I have edited the post accordingly. SuperLittleAdmin 257 — 2y
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 EricAndTheWell 11 — 2y
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? SuperLittleAdmin 257 — 2y
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? SuperLittleAdmin 257 — 2y
0
No, they arent uploaded by myself, looped is checked and no last time I checked there wasnt any EricAndTheWell 11 — 2y
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. SuperLittleAdmin 257 — 2y
Ad

Answer this question