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
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)
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.
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