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