How do I clone sound in my script? (Also this is my post)

Hi, I’m beginner script writing. I been struggle to clone sound (from ServerScriptService) into landmine then destroy in case of lag.
I did try clone() but it didn’t work.

local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")

local bombSound = ServerStorage["Bomb Sound"]

local function makeLandmine(hitbox)
	hitbox.Touched:Connect(function(hit)
		local character = hit:FindFirstAncestorWhichIsA("Model")
		if character then
			local humanoid = character:FindFirstChild("Humanoid")
			if humanoid then
				local explosion = Instance.new("Explosion")
				explosion.Position = hitbox.Position
				explosion.Parent = workspace
				for _, v in pairs(hitbox.Parent:GetChildren()) do
					if v:IsA("BasePart") then
						v.Transparency = 1
						v.CanCollide = false
					end
				end
				hitbox:Destroy()
			end
		end
	end)
end

for _, landmine in pairs(CollectionService:GetTagged("Landmine")) do
	makeLandmine(landmine)
end

Actually, the Sound instance has a pretty convenient property for this called PlayOnRemove.
So in theory you could clone the sound into the landmine, then set the PlayOnRemove to true, and it will automatically play when the landmine gets destroyed.

-- example script for this
local bombSound = game.ServerStorage["Bomb Sound"]
local function makeLandmine(hitbox)
	local sound = bombSound:Clone()
	sound.PlayOnRemove = true
	sound.Parent = hitbox

	-- rest of code goes here
end

(I might’ve gone a little off topic, apologies!)

Yep. The parent is also important it determines the location of the sound.

Also dont forget the :Play() function and checking the volume and sound falloff distance as well. It might be playing but you cant hear it.

Ok! Thank you a lot. Now it work as intended.

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