Issue with a value inside a for loop

i am trying to print/get the t value inside the for loop outside the loop but each time i run the script the green highlighted prints the value i want but the red one only prints nil

my only attempted solution was making a value that was empty outside the for loop so the print would possibly work

local middle = script.Parent:FindFirstChild("middle")
local airsound = script["Air Vent Sound"]

while task.wait() do
	local middleitems = game.Workspace:GetPartsInPart(middle)
	local t = nil
	local function find(middleitems, name)
		for _, v in pairs(middleitems) do
			if v.Name == name then 
				t = v.Pressure.Value
				print(t .. " inside for loop")
				return v 
			end
		end
		return nil
	end
	print(t)
	if find(middleitems, "Pressure") and airsound.Playing == false then
		airsound:Play()
	elseif not find(middleitems, "Pressure") and airsound.Playing == true then
		airsound:Stop()
		print("me me me 2")
	end
end

image

because youre not running the loop before printing t so its printing its original value

so how can i make the loop run and then print t

t is going to be different each time since it’s changing in the loop.

Just run the function or delete the function and just make the for-loop run directly lol.

i cant delete the function because i need the values for code thats running below the screenshot

also just a quick question but how do i get the pressurevalue out of the find :sweat_smile:

Then just call it before printing as I and others have said lol.

And to get what you’re calling of “pressurevalue” out of the find function, just use FindFirstChild I guess.

how do i call a function again i just have to do find(middleitems) right?

A really cool part about functions is that they can return a value. Instead of setting t directly inside the find function, we can set it to be the value returned from find.

-- ... 
local function find(middleitems, name)
	for _, v in pairs(middleitems) do
		if v.Name == name then 
			return v -- we dont need to set t directly just return the value
		end
	end
	return nil
end

local t = find(middleitems, "Pressure") -- Getting the pressure value
if t and airsound.Playing == false then -- we dont need to call find again since we already have t
	airsound:Play()
elseif not t and airsound.Playing == true then -- we dont need to call find again since we already have t
	airsound:Stop()
end
-- ...

Because of the way the find function is now structered, there is no need to keep it inside the loop which not only makes it more readable but also more performant since we are not creating a new find function every frame.

local function find(middleitems, name)
	for _, v in pairs(middleitems) do
		if v.Name == name then 
			return v 
		end
	end
	return nil
end

while task.wait() do
	-- ...
end

Here is your code with all of these changes:

local middle = script.Parent:FindFirstChild("middle")
local airsound = script["Air Vent Sound"]

local function find(middleitems, name)
	for _, v in pairs(middleitems) do
		if v.Name == name then 
			return v 
		end
	end
	return nil
end

while task.wait() do
	local middleitems = game.Workspace:GetPartsInPart(middle)
	local t = find(middleitems, "Pressure")
	print(t)
	if t and airsound.Playing == false then
		airsound:Play()
	elseif not t and airsound.Playing == true then
		airsound:Stop()
		print("me me me 2")
	end
end

Hope this helps! (: