Returning a string comes out as nil when returned?

Hello,
in this script I want to return a string from a function. After multiple testing I still couldn’t find out what was the issue. When I tested it I tried to see if it did go inside my if statements that only returns a simple string and it does but or some reason when I print out to see the results it just gives me a nil ? I am not using any remote function/event so I don’t know why it does this.

Here’s the function that returns

local function ContainsItem(returnItem, returnName)
	local answer = "Plate"
	for v,i in ipairs(FoodList) do
		if Model:FindFirstChild(i) then 
			if returnName then return Model:FindFirstChild(i).Name end
			return true
		end
	end 
	
	if returnItem then 
		--print(table.find(FoodList, Model:FindFirstChildWhichIsA('Tool'):GetChildren()[1]))
		for v,i in ipairs(FoodList) do
			if not Model:FindFirstChildWhichIsA('Tool') and not Model:FindFirstChild(i) then return false end
			warn(Model:FindFirstChildWhichIsA('Tool'))
			warn(#Model:FindFirstChildWhichIsA('Tool'):GetChildren() - 1)
			if Model:FindFirstChildWhichIsA('Tool') and Model:FindFirstChildWhichIsA('Tool'):FindFirstChild('Handle') and #Model:FindFirstChildWhichIsA('Tool'):GetChildren() == 1 then return answer end
			if Model:FindFirstChild('Clean_Plate'):FindFirstChild(i) then return true end
			return false
		end
	end
end

Here’s the part where it’s handling it

local function TakeItem(plr)
	local TempTool = ContainsItem(false, true)
	print(TempTool)
end

The problem is at
if Model:FindFirstChildWhichIsA('Tool') and Model:FindFirstChildWhichIsA('Tool'):FindFirstChild('Handle') and #Model:FindFirstChildWhichIsA('Tool'):GetChildren() == 1 then return answer end
Which I verify even inside the if statement earlier that I did with a printing so logically it’s supposed to return the string value no?

1 Like

the issue is how the function was calledwe can ensure a return value

local function ContainsItem(returnItem, returnName)
    local answer = "Plate"
    
    for _, itemName in ipairs(FoodList) do
        local item = Model:FindFirstChild(itemName)
        if item then
            if returnName then
                return item.Name
            end
            return true
        end
    end
    
    if returnItem then
        local tool = Model:FindFirstChildWhichIsA('Tool')
        if tool then
            if tool:FindFirstChild('Handle') and #tool:GetChildren() == 1 then
                return answer
            end
        elseif Model:FindFirstChild('Clean_Plate') then
            for _, itemName in ipairs(FoodList) do
                if Model.Clean_Plate:FindFirstChild(itemName) then
                    return true
                end
            end
        end
    end
    
    return false
end

and in the part where you handle it

local function TakeItem(plr)
    local TempTool = ContainsItem(true, true)
    print(TempTool)
end