How would I make it so that if something is in the workspace nothing will happen, but if something is in workspace something will happen

Currently trying to make so whenever you join a game, nothing will happen, but if you insert something like a Sound in workspace, then something will happen.

Right now I’ve tried everything but it isn’t working. I’m trying to make it so whenever a sound with a certain name is inserted into workspace, the parts will react. But currently whenever I do this and insert a sound into workspace, nothing happens after even though its supposed to work.

Code:

local parts = game.Workspace.Lights:GetChildren("Light")
local Sound = game.Workspace.KMusic -- This should only trigger when the sound appears in workspace but it currently isn't--

Sound:Play()

while Sound.IsPlaying do
	for _, part in next, parts do
		part.Color = Color3.fromRGB(4, 4, Sound.PlaybackLoudness/4);
	end
	wait();
end

If you want to wait for the sound to appear in the workspace before you play it you can do

local Sound = workspace:WaitForChild("KMusic")

This will wait for the sound to appear in the workspace and then continue the script.

Also you can use this:

local Sound

while true do
   local sound = workspace.ChildAdded:Wait()
   if sound.Name == "KMusic" then
      Sound = sound
      break
   end
end

Try this:

local Workspace = game:GetService("Workspace")
local Sound = Workspace:WaitForChild("KMusic")
local parts = Workspace.Lights:GetChildren("Light")

local CertainName = "Certain Name Here"

Workspace.ChildAdded:Connect(function(Child)
    if Child:IsA("Sound") and Child.Name == CertainName then
       Sound:Play()
       while Sound.IsPlaying do
	       for _, part in next, parts do
		       part.Color = Color3.fromRGB(4, 4, Sound.PlaybackLoudness/4);
	       end
	       wait();
       end
    end
end)

It seems there is an Infinite Yield

WaitForChild says infinite yield if it doesn’t find something in 5 seconds, its just a warning to let you know that it could possibly wait forever if the object never shows up. When it does show up the script will continue.

1 Like

If you want to know when something is added to workspace you can use workspace.ChildAdded event.

workspace.ChildAdded:Connect(function(child) 
	print(child.Name, " was added to workspace")
	
	--Check what is name of child
	if child.Name == "NameOfSound" then 
		--Add code here
		print("Found ",child.Name)
	end
end)

task.wait(10)

--Test adding sound to workspace
local sound = Instance.new("Sound")
sound.Name = "NameOfSound"
sound.Parent = workspace
1 Like