Help with Destroy()

I’m new here and to scripting as well. This is my first post.

Basically, upon clicking a certain part, I want an object from ReplicatedStorage to appear and do its thing. However, it’s going to be destroyed once a certain sound ends. But pretty much nothing happens, it just stays there. I don’t have any video software that I can count on. Just ask me for some screenshots if you need them.

So far, I’ve tried multiple solutions such as watching a couple YouTube tutorials, searching the problem online to see similar issues and try to piece a solution together, but I’ve found nothing. So I decided to go on Devforum to create a topic about this.

By seeing the code you can already tell I’m trying to make an Undertale game, if you know about gaster blasters from Undertale, here’s the extra details for you.

When I click a certain object, the gaster blaster spawns in from ReplicatedStorage to Workspace then fires a beam. Gaster blasters open their mouths while they fire a beam, so after the gaster blaster opens it’s mouth, a sound plays. After the sound ends, I want the gaster blaster to be destroyed.

Here’s the code.

local spawner = script.Parent
local spawned = game.ReplicatedStorage.Gasterblastermiddle
local beam = game.ReplicatedStorage.Gasterblastermiddle.Beamer.Shoot

spawner.ClickDetector.MouseClick:Connect(function()
	spawned:Clone().Parent = game.Workspace
	if beam.Ended then
		spawned:Destroy()
	end
end)
1 Like

I can’t tell you how to create a working blaster and all, but in the code you are trying to find Ended instantly after the blaster gets spawned in the workspace. Since you want it to do its thing before despawning, you can:

  1. Work with attributes and use repeat wait() until spawned:GetAttribute("Attribute") == true to despawn it at the correct time.
    or
  2. Make it do whatever it wants in the same script and despawn it at the end of the code.
2 Likes

Thank you, I’ll try this actually. This seems convincing. I’ll update you if it works or not, I’m not really familiar with attributes so I’ll try to go with the second option.

1 Like

Sure, if you need any help, feel free to ask!

Thank you so much, it actually worked. I simply deleted the portion which checks if the sound plays and destroys it in the script, then went to the blaster script and deleted the while true loop, the “end” for the while true loop, and added “script.Parent:Destroy()” at the end of it. And it actually worked!

1 Like

Glad I was able to help!

character limit eeeeeee

Don’t rely on conditional iterating loops when you could rely on events, it’s bad code practice and places extra emphasis on task scheduler which you really don’t have much control over. It’s also much harder to read.

A more reasonable solution to this problem is to use Sound.Ended:Wait(), because it is event based and will yield your code until the sound ends.

(also, just for your future reference, global wait is subject to be deprecated at any point in time- yield through task library instead).

1 Like

A more reasonable solution to this problem is to use Sound.Ended:Wait() , because it is event based and will yield your code until the sound ends.

So you’re telling me that I could’ve fixed my entire script and get the blaster to work just by using Sound.Ended:Wait()? I feel dumb now.

Roblox is OOP based- every Instance with events that becomes active will eventually index to Event:Wait() provided you call it on some event. IE, yielding for events should happen through Event:Wait() every single time.

local spawner = script.Parent
local storage = game:GetService("Storage")
local spawned = storage:WaitForChild("Gasterblastermiddle")
local beam = spawned:WaitForChild("Beamer"):WaitForChild("Shoot")

spawner:WaitForChild("ClickDetector").MouseClick:Connect(function()
	local spawnedClone = spawned:Clone()
	spawnedClone.Parent = game.Workspace
	if beam.Ended then
		spawnedClone:Destroy()
	end
end)

You just needed to assign the cloned instance to a variable so that it can be correctly referenced in the script.

Oh, I see. Guess I’m just dumb.

Nah, it’s actually a mistake I see quite often.