Am I playing sounds effects correctly?

Hello there,

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?


task.spawn(function() 
	local _sfx = Remote.Sounds.SfxExplosion:Clone()
	_sfx.Parent = _explodingObject.Parent
	_sfx:Play()
	_sfx.Ended:Wait()
	_sfx:Remove()
end)	

Thank you in advance!

2 Likes

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().

1 Like

:Wait() takes no parameters. If it gets parameters, it doesn’t do anything with it.

??

2 Likes

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:

humanoid:MoveTo(npcPosition+Vector3.new(5,0,0))
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(npcPosition+Vector3.new(5,0,0))

Edit: This used to be an issue for me in the past, but I believe Roblox might’ve done a fix for it. Putting a 0 in there doesn’t do anything.

image

I don’t know what you’re trying to say.

2 Likes

replace Remove() with Destroy() and it must work, _sfx:Remove() is not a valid function

1 Like

I’ve been using :Remove() for a long time now, and it seems to be working just fine. What is the reason to use :Destroy() over :Remove()?

idk:

1 Like

In a case like this where you are destroying the sound after it’s played I would suggest doing this to be more efficient:

local _sfx = Remote.Sounds.SfxExplosion:Clone()
_sfx.Parent = _explodingObject.Parent
_sfx.PlayOnRemove = true
_sfx:Destroy()
1 Like

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.

1 Like

Using :Remove() will cause a memory leak in most cases.
Use :Destroy() unless you plan on reusing something.

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.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.