Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Audio Cloned to a Part isn't Playing?

I have this aerial sound system that uses parts positioned around the player. It then picks a random part, random sound, and plays the sound to the part.

The problem is that it gets to the point where it clones the sound (I think) but it just doesn't play the sound.

I think it's the way I'm finding the sound to copy but I've tried a few different things and they never worked.

script:

local parts = {
	parta = Instance.new('Part', workspace),
	partb = Instance.new('Part', workspace),
	partc = Instance.new('Part', workspace),
	partd = Instance.new('Part', workspace),
	parte = Instance.new('Part', workspace),
	partf = Instance.new('Part', workspace)
}	

for i,v in pairs(parts) do
	v.Anchored = true
	v.CanCollide = false
	v.Transparency = .5
end


while true do
	wait(math.random(2,3))
	local character = game.Players.LocalPlayer.Character
	if character then
		parts.parta.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(-20,0,0)
		parts.partb.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(20,0,0)
		parts.partc.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,-20,0)
		parts.partd.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,20,0)
		parts.parte.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,0,-20)
		parts.partf.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,0,20)
		
		local numeral = math.random(1,6)
		local ran = math.random(1,2)
		local c = parts["parts"..numeral]
		if character.HumanoidRootPart.Position.Z < workspace.maincheckpoint.Position.Z then
			print('going along')
			if ran == 1 then
				print('jump')
				local thingstocopy = workspace.sounds.ambient.jump:GetChildren()
				local thingtocopy = thingstocopy[math.random(#thingstocopy)]
				local copy = thingtocopy:Clone()
				copy.Parent = c
				wait(.3)
				copy:Play()
				wait(copy.TimeLength)
				wait(.1)
				copy:Destroy()
			end
			if ran == 2 then
				print('subtle')
				local thingstocopy = workspace.sounds.ambient.subtle:GetChildren()
				local thingtocopy = thingstocopy[math.random(#thingstocopy)]
				local copy = thingtocopy:Clone()
				copy.Parent = c
				wait(.3)
				copy:Play()
				wait(copy.TimeLength)
				wait(.1)
				copy:Destroy()
			end
		end	
	end
end

1 answer

This was resolved by keying the dictionary and using that

Fixed version (in case of references needed by other developers):

local parts = {
	parta = Instance.new('Part', workspace),
	partb = Instance.new('Part', workspace),
	partc = Instance.new('Part', workspace),
	partd = Instance.new('Part', workspace),
	parte = Instance.new('Part', workspace),
	partf = Instance.new('Part', workspace)
}	

local partkeys = {}

for i,v in pairs(parts) do
	v.Anchored = true
	v.CanCollide = false
	v.Transparency = .5
	table.insert(partkeys, v)
end


while true do
	wait(math.random(2,3))
	local character = game.Players.LocalPlayer.Character
	if character then
		parts.parta.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(-20,0,0)
		parts.partb.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(20,0,0)
		parts.partc.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,-20,0)
		parts.partd.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,20,0)
		parts.parte.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,0,-20)
		parts.partf.CFrame = character.HumanoidRootPart.CFrame - Vector3.new(0,0,20)
		
		local numeral = math.random(1,6)
		local ran = math.random(1,2)
		local c = partkeys[math.random(#partkeys)]
		if character.HumanoidRootPart.Position.Z < workspace.maincheckpoint.Position.Z then
			print('going along')
			if ran == 1 then
				print('jump')
				local thingstocopy = workspace.sounds.ambient.jump:GetChildren()
				local thingtocopy = thingstocopy[math.random(#thingstocopy)]
				local copy = thingtocopy:Clone()
				copy.Parent = c
				wait(.3)
				copy:Play()
				wait(copy.TimeLength)
				wait(.1)
				copy:Destroy()
			end
			if ran == 2 then
				print('subtle')
				local thingstocopy = workspace.sounds.ambient.subtle:GetChildren()
				local thingtocopy = thingstocopy[math.random(#thingstocopy)]
				local copy = thingtocopy:Clone()
				copy.Parent = c
				wait(.3)
				copy:Play()
				wait(copy.TimeLength)
				wait(.1)
				copy:Destroy()
			end
		end	
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.