Why do I get this error?

I’m using the script bellow to play Ambiant sound but I keep getting this error.

:09:14.105  Players.MasonX890.PlayerScripts.ANI.ANI_Ambiant:39: attempt to index nil with 'Position'  -  Client - ANI_Ambiant:39
  13:09:14.105  Stack Begin  -  Studio
  13:09:14.105  Script 'Players.MasonX890.PlayerScripts.ANI.ANI_Ambiant', Line 39 - function CheckOceanDistance  -  Studio - ANI_Ambiant:39
  13:09:14.105  Script 'Players.MasonX890.PlayerScripts.ANI.ANI_Ambiant', Line 135  -  Studio - ANI_Ambiant:135
  13:09:14.105  Stack End  -  Studio
local LocalPlayer = game.Players.LocalPlayer;
local SoundService = game.SoundService;
local x1 = nil;
local x2 = nil;
local x3 = nil;
local x4 = nil;
local function Find(player1)
	while true do
		wait();
		if player1:IsDescendantOf(workspace) then
			break;
		end;	
	end;
	x1 = player1;
	x2 = player1:WaitForChild("Humanoid");
	x3 = player1:WaitForChild("HumanoidRootPart");
	x4 = player1:WaitForChild("UpperTorso");
end;
if LocalPlayer.Character then
	Find(LocalPlayer.Character);
end;
LocalPlayer.CharacterAdded:connect(Find);
function BuildingCheck()
	local __CFrame = x3.CFrame;
	local CF, Pos = workspace:FindPartOnRayWithIgnoreList(Ray.new(__CFrame.p, (x3.Position + Vector3.new(0, 10, 0) - __CFrame.p).unit * 100), { workspace.Ignore, x1, workspace.ObjectSelection, workspace.Water }, false, true);
	if CF then
		return true;
	end;
	return false;
end;
function CheckOceanDistance()
	local Coast = workspace.Ignore.WorldObjects.Coastline:GetChildren();
	local HUGE = math.huge;
	local Coast2 = #Coast;
	local Number = 1 - 1;
	while true do
		local Magnitude = (Coast[Number].Position - x3.Position).Magnitude;
		if Magnitude < HUGE then
			HUGE = Magnitude;
		end;
		if 0 <= 1 then
			if Number < Coast2 then

			else
				break;
			end;
		elseif Coast2 < Number then

		else
			break;
		end;
		Number = Number + 1;	
	end;
	if HUGE <= 1200 then
		return true;
	end;
	return false;
end;
function Fade(player2, player3, player4)
	if player3 then
		local Num2 = player4 - -0.2;
		while true do
			wait();
			player2.Volume = Num2;
			if 0 <= -0.2 then
				if Num2 < 0 then

				else
					break;
				end;
			elseif 0 < Num2 then

			else
				break;
			end;
			Num2 = Num2 + -0.2;		
		end;
		player2.Volume = 0;
		return;
	end;
	local Num3 = 0 - 0.2;
	while true do
		wait();
		player2.Volume = Num3;
		if 0 <= 0.2 then
			if Num3 < player4 then

			else
				break;
			end;
		elseif player4 < Num3 then

		else
			break;
		end;
		Num3 = Num3 + 0.2;	
	end;
	player2.Volume = player4;
end;
while true do
	wait(0.1);
	if x1 ~= nil and x3 ~= nil then
		local Time = game.Lighting:GetMinutesAfterMidnight();
		if BuildingCheck() then
			if SoundService.RoomAmbient.Volume <= 0 then
				Fade(SoundService.RoomAmbient, false, 1);
			end;
			if SoundService.OceanAmbient.Volume > 0 then
				Fade(SoundService.OceanAmbient, true, 0.5);
			end;
			if SoundService.NightAmbient.Volume > 0 then
				Fade(SoundService.NightAmbient, true, 2);
			end;
			if SoundService.DayAmbient.Volume > 0 then
				Fade(SoundService.DayAmbient, true, 2);
			end;
		else
			if Time > 370 and Time + 1 < 1090 then
				if SoundService.DayAmbient.Volume <= 0 then
					Fade(SoundService.DayAmbient, false, 2);
				end;
				if SoundService.NightAmbient.Volume > 0 then
					Fade(SoundService.NightAmbient, true, 2);
				end;
			else
				if SoundService.NightAmbient.Volume <= 0 then
					Fade(SoundService.NightAmbient, false, 2);
				end;
				if SoundService.DayAmbient.Volume > 0 then
					Fade(SoundService.DayAmbient, true, 2);
				end;
			end;
			if CheckOceanDistance() then
				if SoundService.OceanAmbient.Volume <= 0 then
					Fade(SoundService.OceanAmbient, false, 0.5);
				end;
			elseif SoundService.OceanAmbient.Volume > 0 then
				Fade(SoundService.OceanAmbient, true, 1);
			end;
			if SoundService.RoomAmbient.Volume > 0 then
				Fade(SoundService.RoomAmbient, true, 1);
			end;
		end;
	end;
end;

It’s because Position isn’t a member of sounds.

Try putting the sound into somewhere near the player then using rolloff modes.

There are tutorials on Youtube for rolloff modes.

Thid is the line were my error is, It not trying to get the Position of sounds

I think it’s because Magnitude is a property of position. Though i’m not sure.

I see that you aren’t looping through the objects? :GetChildren() wont magically have a position. its an array. You may have to loop through it?

EDIT: FYI Roblox’s starts at 1. Not 0. Also mess with the Indexing