ModuleScript return nil instead of instance

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to get instance using ModuleScript

  2. What is the issue? Include screenshots / videos if possible!
    It return nil instead of the instance that I wanted

And anyways, here is the screenshot
asdweasdads

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    My solution doesn’t work, If you’re wondering what is my solution then.
-- This is just a fraction of the code
FindSomething.FoundedStuff = nil

if Child.Name == Stuff then
	print(Child.Name, "has been found at "..Child.Parent.Name.." and the service that it was found at is "..Service.Name..". Deep Search attempts : "..DeepSearchAttempts)
	DeepSearchAttempts = 0
					
	if ReturnIt ~= false then
		FindSomething.FoundedStuff = Child
		return FindSomething.FoundedStuff
	else
		break
	end
else
	DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
end

And also I have tried looking for solutions

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Sorry for the messy script, I don’t have time to clean it up.

ModulueScript :

local FindSomething = {}
FindSomething.Services = {
	game:GetService("Workspace"),
	game:GetService("ServerStorage"),
	game:GetService("ServerScriptService"),
	game:GetService("Players"),
	game:GetService("ReplicatedStorage"),
	game:GetService("StarterGui")
}
FindSomething.DeepSearchAttempts = 0

local function GiveStuff()
	
end

local function DeepSearch(SomeStuff, UsingName, ReturnIt, Stuff, Service)
	FindSomething.DeepSearchAttempts += 1
	
	if UsingName ~= false then
		if #SomeStuff:GetChildren() > 0 then
			for _, Child in pairs(SomeStuff:GetChildren()) do
				if Child.Name == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name.." and the service that it was found at is "..Service.Name..". Deep Search attempts : "..FindSomething.DeepSearchAttempts)
					FindSomething.DeepSearchAttempts = 0
					
					if ReturnIt ~= false then
						print(Child)
						return Child
					else
						break
					end
				else
					DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
				end
			end
		end
	else
		if #SomeStuff:GetChildren() > 0 then
			for _, Child in pairs(SomeStuff:GetChildren()) do
				if Child == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name.." and the service that it was found at is "..Service.Name..". Deep Search attempts : "..FindSomething.DeepSearchAttempts)
					FindSomething.DeepSearchAttempts = 0
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
				end
			end
		end
	end
end

local function Search(Stuff, UsingName, ReturnIt)
	if UsingName ~= false then
		for _, Service in pairs(FindSomething.Services) do
			for _, Child in pairs(Service:GetChildren()) do
				if Child.Name == Stuff then
					print(Child.Name, "has been found at "..Service.Name..".")
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
				end
			end
		end
	else
		for _, Service in pairs(FindSomething.Services) do
			for _, Child in pairs(Service:GetChildren()) do
				if Child == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name..".")
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
				end
			end
		end
	end
end

function FindSomething:Find(Stuff)
	return Search(Stuff, false, false)
end

function FindSomething:FindUsingName(Stuff)
	return Search(Stuff, true, false)
end

function FindSomething:FindAndReturnIt(Stuff)
	return Search(Stuff, false, true)
end

function FindSomething:FindUsingNameAndReturnIt(Stuff)
	local ItemFound = Search(Stuff, true, true)
	print("From Function" , ItemFound)
	return ItemFound
end

return FindSomething

Script :

local FindSomething = require(game.ReplicatedStorage:FindFirstChild("FindSomething"))

local YourMother = FindSomething:FindUsingNameAndReturnIt("YourMother")

print(YourMother)
print(YourMother.Name, "has died.")

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Please tell me if this topic is in the wrong category.

I think you should Change from ( : ) Method to use ( . ) .

Ex :

FindSomething.FindUsingNameAndReturnIt(Stuff)

But if you use

FindSomething:FindUsingNameAndReturnIt( Self , Stuff)

Because ( : ) use for method it will auto set as Self In first Item.

I Recommend use ( . ) for function in module , It’s more ez

Hope This Help

1 Like

I will try to do what you just said.

Now it’s basically just print this ( when using . )
fsgfddfhg
instead of ( when using : )
dsfsdfsdf

In your module function you didn’t return the value when u found it

Maybe let put return For Search

Ex:

function FindSomething.FindUsingNameAndReturnIt(Stuff)
Return Search(Stuff, true, true)
end

Let me try the “Return Search(Stuff, true, true)” one

it’s still doesn’t work though.

wait I am forgetting something

Nevermind, it still doesn’t work

Last Thing you can try XD

function FindSomething.FindUsingNameAndReturnIt(Stuff)
local ItemFound = Search(Stuff, true, true)
print( “From Function” , ItemFound)
return ItemFound
end

if the value printed is nil so it might be the error of Search

1 Like

It’s print nil, so yes it’s my goofy ModuleScript’s problem
asdasdargsa

if ReturnIt ~= false then
	print(Child) -- It does print the instance
	return Child -- this one must be the source of the problem
else
	break
end

Oki Oki Final I Guess

Put Return for all DeepSearch and Search

like

return DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)

return Search(Stuff, true, true)

Because I think the value that you return in someplace isn’t return to the main function one because you use it override like that

To fix this issue, you can modify the DeepSearch function to return the result of the recursive call to itself when the expected instance is not found.

Here’s the updated deepsearch function:

local function DeepSearch(SomeStuff, UsingName, ReturnIt, Stuff, Service)
	FindSomething.DeepSearchAttempts += 1
	
	if UsingName ~= false then
		if #SomeStuff:GetChildren() > 0 then
			for _, Child in pairs(SomeStuff:GetChildren()) do
				if Child.Name == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name.." and the service that it was found at is "..Service.Name..". Deep Search attempts : "..FindSomething.DeepSearchAttempts)
					FindSomething.DeepSearchAttempts = 0
					
					if ReturnIt ~= false then
						print(Child)
						return Child
					else
						break
					end
				else
					local result = DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
					if result then
						return result
					end
				end
			end
		end
	else
		if #SomeStuff:GetChildren() > 0 then
			for _, Child in pairs(SomeStuff:GetChildren()) do
				if Child == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name.." and the service that it was found at is "..Service.Name..". Deep Search attempts : "..FindSomething.DeepSearchAttempts)
					FindSomething.DeepSearchAttempts = 0
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					local result = DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
					if result then
						return result
					end
				end
			end
		end
	end
end
1 Like

Thank @Swetch29 and @phearothphearayuth for helping me fixing my goofy “Finder Stuff ModulueScript”. I am so grateful that both of you helped me.

1 Like

I don’t know what is wrong with my code, it only work once somehow?

The normal “Search” is working fine, but somehow “DeepSearch” only work once?

Okay, so I have fixed it. Turn out the “Search” need to be changed too.
This

local function Search(Stuff, UsingName, ReturnIt)
	if UsingName ~= false then
		for _, Service in pairs(FindSomething.Services) do
			for _, Child in pairs(Service:GetChildren()) do
				if Child.Name == Stuff then
					print(Child.Name, "has been found at "..Service.Name..".")
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
				end
			end
		end
	else
		for _, Service in pairs(FindSomething.Services) do
			for _, Child in pairs(Service:GetChildren()) do
				if Child == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name..".")
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
				end
			end
		end
	end
end

to this

local function Search(Stuff, UsingName, ReturnIt)
	if UsingName ~= false then
		for _, Service in pairs(FindSomething.Services) do
			for _, Child in pairs(Service:GetChildren()) do
				if Child.Name == Stuff then
					print(Child.Name, "has been found at "..Service.Name..".")
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					local Result = DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
					
					if Result then
						return Result
					end
				end
			end
		end
	else
		for _, Service in pairs(FindSomething.Services) do
			for _, Child in pairs(Service:GetChildren()) do
				if Child == Stuff then
					print(Child.Name, "has been found at "..Child.Parent.Name..".")
					
					if ReturnIt ~= false then
						return Child
					else
						break
					end
				else
					local Result = DeepSearch(Child, UsingName, ReturnIt, Stuff, Service)
					
					if Result then
						return Result
					end
				end
			end
		end
	end
end
1 Like

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