First time poster, new at Roblox and Lua, looking for feedback on this tiny bite of code that plays a sound effect. The sound is played when a specific object (pickup or a trap) is activated and usually destroyed afterwards, so I parent the sound to the object parent (it’s a platform where the object is placed).
I’m mostly concerned with the :Clone() part, because it feels like it would have a negative effect on performance.
Potential fix would be checking if the platform (parent of the pickup or the trap) already has a sound effect kiddo and using that instead of a clone. That way I could also drop the Wait(), Remove() and probably do without task.spawn(). So my question is… Would that be a better approach?
It looks just fine, but there’s a few things I want to point out: When you use :Wait(), putting a 0 in it like :Wait(0) makes the code underneath run instantly after the sound has ended, instead I believe there’s a small task.wait() added to it without the 0, and it’s :Destroy(), not :Remove().
Try making an NPC move forward 4 studs, put a humanoid.MoveToFinished:Wait() (without the 0) and make it move 4 more studs, and you’ll see it stops for a split second. It should look something like this:
I mean, there is no harm in checking if there is a “kiddo” sound effect already there.
You could probably use in if statement to check for an pre-existing sound child and either Destroy:() it and continue cloning from Remote or you could simply play that child instead.
However, your code seems to always delete it after it’s ended so this is not nessecary.
On the other hand, in a larger scale of things it is always a good practice to check. Imagine the code is interupoed before it can delete it. You night want to implement that as a way to avoid lots of clones sounds as a child of on object.
I wouldn’t worry about performance tolls with either scenario, as I don’t think it will be a major factor.
However, where are you cloning these sounds from? What is “Remote”? Just curious.
Remote is just how I named ReplicatedStorage, because I liked how Remote.Events sounds. I have some weird quirks about how I write code, so it’s good that I work solo.
Didn’t even consider that things can be interrupted while the sound is playing and I also didn’t know that I wasn’t supposed to use Remove(), so creating this thread was a really good idea.