0

Using the Child-monitoring script @Kylaaa helped me with in another question, I've spent hours on this, altering the core functionality from healing and disappearing bricks to play a parent sound when the player "walks through the leaves". I've been studying various aspects of the problem ('IsA', 'TouchInterest', 'If...then', Classes, Operators, etc.), and done various things, such as altering my script to look at the name of touched objects instead of their classes, but I've had no luck getting the sound to play.

The script

-- create a helper function to access the brick and the thing that touched it
function OnTouched(thing)
    local active = false --start with this debounce variable off so that the function will run the first time
    return function(target) --provides actual target of the event
        if active then return end       -- do not do the animation again if it has already started
        local player=thing.Parent:findFirstChild("Humanoid") --determine if it's humanoid
        if player then --if it is, then
            active = true --toggle to prevent function from running again simultaneously
            script.Parent:play() --play rustling leaves sound
            wait(1)
            active = false -- reset so function can be used again
        end --end of if statement
    end
end

-- loop over the children and connect touch events
local bricks = script:GetChildren()
for i, brick in ipairs(bricks) do
    if brick:IsA("Part") then
        local onTouchedFunc = OnTouched(brick)
        brick.Touched:Connect(onTouchedFunc)
    end
end

This is a shot of the leaf litter I want to add the rustling sound to. I also have the same problem with the vines on the left side.

leafy area

Vines:

Vines structure and properties

All leaves are 'MeshParts' with 'SurfaceAppearance' inside, and are Anchored and CanTouch but not CanCollide. I tried turning on CanCollide but it didn't help.

Dense Leaf patch properties:

Dense Leaf patch properties

Surface Appearance properties:

Surface Appearance properties

I had tried "if brick:IsA("Model" or "MeshPart") then" and "if brick.Name=="Dense Leaf patch" or "SurfaceAppearance" or "DenseLeafPatch" then" but neither of those worked.

I then noticed that a TouchInterest wasn't generated by Roblox, and I learned that you can neither copy nor duplicate TouchInterests to use where you need them (they're not there for developers, according to a message in the DevForum).

I originally had 1-3 leaves 'MeshPart's in separate 'Model's, but then made a single, invisible part (name: TouchInterest), CanTouch not CanCollide, that was large enough to cover almost all of the physical area of the instances, and put all of the MeshParts into that part. Then, I altered the script to just look for 'Part's. As a result, a TouchInterest does get generated during run-time.

Shows the invisible part all leaves are in.

This is all of the stuff involved in the group "leaves", with the sound, then the script, the containing part, then all the leaves inside as children of the script.

Structure and properties of the containing part

Sound's properties:

Properties of the sound

No errors for this were in the Output.

I feel like there's something simple I don't understand. Do I need several small parts (instead of the one large one) encompassing groups of leaves? Please help!

GAM II
  • 45
  • 8
  • You've said that the sound doesn't play, but have you tried adding breakpoints or `print` statements to step through the code? Is the `brick.Touched` event firing properly when you touch any of the leaves or the TouchInterest part? Does the code reach the call to `script.Parent:Play()`? If you inspect the Sound in the Explorer when you are playing, can you see that it's playing? Is the volume turned up on your computer and in Roblox Studio? – Kylaaa Oct 28 '21 at 22:58
  • I don't understand what you mean about breakpoints, sorry. I will try print statements and let you know what happens. – GAM II Oct 29 '21 at 02:54
  • The volume on the sound is 10, and I set it to "Play" and could hear it as the game loaded. I found an error on line 6 where I used `thing` instead of `target`, so I fixed that. I put prints everywhere and could see the script running. The first time it was triggered while "Play" was on, it actually played the sound, but it didn't play it again. The print output shows that, after that, the return function was triggered (everything after :play() was skipped), and then it looped between finding that debounce was on and the return function triggering. The wait print didn't trigger. – GAM II Oct 29 '21 at 03:58
  • @Kylaaa So, actually, wait(1) DID trigger...After I stopped moving, and then it turned off debounce and exited. What the heck? The output is too much for the comment box, and I can't add an image... I went back in and it is different because I rem'd out the wait statement. It now IGNORES the debounce and triggers every time I move, making it sound like a disaster. – GAM II Oct 29 '21 at 04:03
  • And then, if I stop moving, and then move again, there's a delay before the sound starts being played. – GAM II Oct 29 '21 at 04:06
  • I capitalized "Play" and added wait(2) after it, and now it's working better. There's still a delay before the sound plays after I move, but with wait at 2 seconds, it doesn't cause the sound to stutter. Why does the return function trigger AFTER the sound playing? I just notice I said line 6 previously - it's actually line 10. – GAM II Oct 29 '21 at 04:13
  • @Kylaaa Ok, I turned down the volume cuz it was an earache, and disabled `Playing`. Interestingly, it never returns back to the first part where the function is called. I guess that's how that works...? There is still a significant delay sometimes, so I'm wondering if this an Internet lag effect, but other times the delay is perhaps .2 sec. So, now the question is: can I get rid of the invisible `BasePart` and just detect the `MeshPart`, or won't Roblox allow that sort of detection? Hmmm....Time to sleep. – GAM II Oct 29 '21 at 04:23
  • Nope, it doesn't detect `MeshPart`s. – GAM II Oct 29 '21 at 04:26
  • I did try using `TouchEnded` on a different script but couldn't figure out to make it work in this `child`-based script. – GAM II Oct 29 '21 at 04:28

1 Answers1

1

I figured out the problems.

  1. On line 6, I used "thing" when I should've used "target".
  2. On line 9, I forgot to capitalize "Play".
  3. On line 10, I needed to make the wait 2 seconds instead of 1.
GAM II
  • 45
  • 8
  • Rather than waiting for 2 seconds, is it worth waiting for the [length of the sound](https://developer.roblox.com/en-us/api-reference/property/Sound/TimeLength) `wait(script.Parent.TimeLength)` – Kylaaa Oct 29 '21 at 14:19
  • I don't know. Although it was working fine last time I wrote, it's no longer working, which much be because of the near-total system-wide outage that started last night. The R app doesn't connect, I cannot access our online games-in-progress (error regarding GUAC policies and UniversalAppConfiguration, I cannot publish, the virus-laden toolbox is down, NPCs are either missing or incomplete, meshes, models, sounds, etc. cannot load, and so on...You know better than I do how hosed Roblox is right now! Good luck! – GAM II Oct 30 '21 at 02:48