How to Iterate Through Instances in a Table?

I need it so that when the variable InRange comes back true, the script will iterate through the table InteractableObjects only once and print whatever instance it succeeds in finding if it’s an ancestor of the current mouse.Target

The problem is nothing gets printed. I’ve tried adding GetChildren and GetDescendants to the table check as other attempts but they come back nil. Tried manually creating the variables for Tree and Apple then having them referenced in a dictionary via putting them in [] but nothing. Searching for other devforum threads hasn’t come up with anything of use I can apply.

New to scripting and tables so please try to keep answers simple if possible.

	--// VARIABLES //--
-- SERVICES
Players = game:GetService("Players")
-- 
selfPlayer = Players.LocalPlayer
mouse = selfPlayer:GetMouse()
-- 
InRange = false
-- TABLES
InteractableObjects = {
	Tree = mouse.Target:FindFirstAncestor("Tree"),
	Apple = mouse.Target:FindFirstAncestor("Apple"),
}

	--// INITIALIZATION //--
repeat
	wait(.1)
until selfPlayer.Character:WaitForChild("HumanoidRootPart") and selfPlayer.Character.Humanoid.Health > 0
character = selfPlayer.Character


	--// FUNCTIONS //--
function RangeCheck()
	if mouse.Target then
		local magnitude = (mouse.Target.Position - character.HumanoidRootPart.Position).Magnitude
		if magnitude <= 4 then
			InRange = true
			else
			InRange = false
			end	
		else InRange = false
		end
	end

	--// EXECUTION //--
while true do
	RangeCheck()
	if InRange == true and character.Humanoid.Health > 0 then
		for i,v in ipairs(InteractableObjects) do
			print(i,v)
			end
		end
	wait(.1)
	end

You never update the InteractableObjects table, so Tree and Apple are both nil when this first loads unless you’re super lucky with your mouse placement, and never get set again.

How would I go about resetting the entire dictionary at once without manually declaring each variable again? So that Tree, Apple, etc will automatically be rechecked and re-added to the table without needing a new line written for each object?

You could define them as functions and call the function each time,

InteractableObjects = {
	Tree = function()
        return mouse.Target and mouse.Target:FindFirstAncestor("Tree")
    end,
	Apple = function()
        return mouse.Target and mouse.Target:FindFirstAncestor("Apple")
    end,
}

--......
		for i, v in pairs( InteractableObjects ) do
			print( i, v() )
			end
		end

It’s also important to use pairs, not ipairs, as you have key-value pairs rather than an array-like table with sequential numerical keys.

As you can see, instead of printing v, I’ve printed v() which will call the function and use its result.