Scream SFX not playing!

Hello! I am trying to make a script that makes it so if a player is ragdolled, it will play a random scream sound that is under the players head if no other scream sound is currently playing. I think I set up the script right, but I hear no sound when a player is ragdolled! There is no errors in the output, the Ragdolled value is set to true, and my only guess is that a if statement may not be running properly. Here is my code:

while wait(0.01) do
	for i,Characters in pairs(game.Workspace:GetChildren()) do
		if Characters:IsA("Model") then
			if Characters:FindFirstChild("Head") then
				if Characters:FindFirstChild("Head"):FindFirstChild("Brain") then
					if Characters:FindFirstChild("Ragdolled").Value == true then
						for i,Screams in pairs(Characters:FindFirstChild("Head"):GetChildren()) do
							local SCREAMS_TABLE = {}
							if table.find(SCREAMS_TABLE,Screams) then
								
							else
								if Screams:IsA("Sound") then
									table.insert(SCREAMS_TABLE,Screams)
								end
							end
							if Screams:IsA("Sound") and #SCREAMS_TABLE == 7 then
								if Screams.Playing == true then
									repeat
										wait()
									until
									Screams.Playing == false

									task.wait(3) -- scream debounce
									local RandomScream = math.random(1,#SCREAMS_TABLE)
									local ScreamSelected = table.find(SCREAMS_TABLE,RandomScream)

									Screams:FindFirstChild(ScreamSelected.Name):Play()
								end
							end
						end 
					end
				end
			end
		end
	end
end

Just a note, Head:FindFirstChild(“Brain”) is apart of my custom rig, you can ignore that. Any help is appreciated!

I don’t see anywhere you’re using the :Play() method to actually play the SFX. Perhaps add that?

Also a tip, try using the conjunction operator (and) to eliminate that massive “if” pyramid you have there.

I couldn’t shorten it much as if I do it gives me a index nil error in the output, but I shortened it a bit. Thank you for reminding me to also put a :Play() again as it seems i forgot to play the sound if no other sounds are playing with a else, but it still doesn’t work.

New code:

while wait(0.01) do
	for i,Characters in pairs(game.Workspace:GetChildren()) do
		if Characters:IsA("Model") and Characters:FindFirstChild("Head") and Characters:FindFirstChild("Ragdolled") then
			if Characters:FindFirstChild("Head"):FindFirstChild("Brain") then
				if Characters:FindFirstChild("Ragdolled").Value == true then
					for i,Screams in pairs(Characters:FindFirstChild("Head"):GetChildren()) do
						local SCREAMS_TABLE = {}
						if table.find(SCREAMS_TABLE,Screams) then

						else
							if Screams:IsA("Sound") then
								table.insert(SCREAMS_TABLE,Screams)
							end
						end
						if Screams:IsA("Sound") and #SCREAMS_TABLE == 7 then
							if Screams.Playing == true then
								repeat
									wait()
								until
								Screams.Playing == false

								task.wait(3) -- scream debounce
								local RandomScream = math.random(1,#SCREAMS_TABLE)
								local ScreamSelected = table.find(SCREAMS_TABLE,RandomScream)

								Screams:FindFirstChild(ScreamSelected.Name):Play()
							else
								local RandomScream = math.random(1,#SCREAMS_TABLE)
								local ScreamSelected = table.find(SCREAMS_TABLE,RandomScream)

								Screams:FindFirstChild(ScreamSelected.Name):Play()
							end
						end
					end 
				end
			end
		end
	end
end

Thanks to REC0NM4N for helping me through discord, him and me were able to fix the script and he even taught me some new stuff! (I had no idea what breakpoints were even used for up until now.) If you want to read through the new code and maybe learn something yourself, here:

local Chars = {}

for i,Character in pairs(game:GetService("Workspace"):FindFirstChild("CHARACTERS"):GetChildren()) do
	if Character:IsA("Model") and Character:FindFirstChild("Head") and Character:FindFirstChild("Ragdolled") and Character:FindFirstChild("Head"):FindFirstChild("Brain") then
		table.insert(Chars,Character)
	end
end

for i,v in pairs(Chars) do
	v.Ragdolled.Changed:Connect(function()
		if v.Ragdolled.Value == true then
			local SCREAMS_TABLE = v:FindFirstChild("Head"):FindFirstChild("SOUNDS"):GetChildren()
			local RandomScream = math.random(1,#SCREAMS_TABLE)
			SCREAMS_TABLE[RandomScream]:Play()
		end
	end)
end

(I gave him the solution because he was the one who pretty much made the new code.)

1 Like

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