Why this script doesn't work in a LocalScript

This script is just so simple that i can’t really figure out what it’s wrong with it, it works (kind of) while using it on the ServerSide but while doing it in a LocalScript the only Print that works its the AmbientFolder

local AmbientFolder = workspace:WaitForChild("AmbientSounds",2)
local TweenS = game:GetService("TweenService")
local ActSound = nil
print(AmbientFolder)

local function TweenSound(ActualSound,nextSound,SoundVolume)
	print("function")
	if ActualSound ~= nil then
		local s1 = TweenS:Create(ActualSound,TweenInfo.new(1),{Volume = 0})
		s1:Play()
		s1.Completed:Wait()
	end
	local s2 = TweenS:Create(nextSound,TweenInfo.new(1),{Volume = SoundVolume})
	s2:Play()
	ActSound = nextSound
end

for _,soundPart in AmbientFolder:GetChildren() do
	if not soundPart:IsA("BasePart") then continue end
	print("soundPart")
	local MaxVolume = soundPart:WaitForChild("Sound").Volume
	soundPart.Touched:Connect(function()
		if ActSound == soundPart.Sound then return end
		print("Touched")
		soundPart.Sound:Play()
		TweenSound(ActSound,soundPart.Sound,MaxVolume)
	end)
end
1 Like

Might be wrong on this one but i think “continue” ends the whole for loop if the first thing it finds isn’t a BasePart

Try disabling streaming enabled in workspace, maybe your locals cript cant reach some parts

He said only the folder is being printed so it doesn’t even reach the yielding part. The problem is the continue at the start of the loop

for _,soundPart in AmbientFolder:GetChildren() do
	if soundPart:IsA("BasePart") then
	  print("soundPart")
	  local MaxVolume = soundPart:WaitForChild("Sound").Volume
	  soundPart.Touched:Connect(function()
		  if ActSound == soundPart.Sound then return end
		  print("Touched")
		  soundPart.Sound:Play()
		  TweenSound(ActSound,soundPart.Sound,MaxVolume)
	  end)
   end
end

Try this instead of using continue

that was the problem, but i can’t really find how to avoid that error, at least i know what cause the problem

Yeah, wrong. The continue keyword just skips the current value in the loop and goes on to the next one, so it doesn’t stop the loop.

2 Likes

Probably confused it with break then, my bad

Yeah, break breaks the for loop, while continue just continues to the next element/value. No problem, just wanted to clarify so there’s no confusion.

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