Function focused on one thing

I know what the problem is, I just don’t know how to fix it. I am trying to make a dialouge system where depending on where tool(fruit) spawns, that is where the npc will give a hint at. I assume the problem is when fruit found = true, I guess the script stays on the function forever. I tried using repeat until loops but that didn’t work. I have been trying to fix this problem for a long time now. Here is the script:

local DialogChoiceModule = {} 

local chosen_spawner 
local tool
local foundFruit 

function DialogChoiceModule.TrackFruit()
	for _,spawner in pairs(game.Workspace.Spawners:GetChildren()) do --Get table of spawners
		if string.match(spawner.Name,"FruitSpawner") then
			if spawner:FindFirstChildWhichIsA("Tool") and chosen_spawner == nil then
				print("Spawner with tool found!")
				chosen_spawner = spawner
				tool = chosen_spawner:FindFirstChildWhichIsA("Tool")
					if tool.Parent == chosen_spawner then
						foundFruit = true
					elseif tool.Parent ~= chosen_spawner then
						foundFruit = false
						chosen_spawner = nil 
				end
			end
		end
	end
end


function DialogChoiceModule.DialogChoice(npc)
	local dialog = npc.Head:FindFirstChild("Dialog")
	local dialogChoice = dialog:FindFirstChild("DialogChoice")
	
		if foundFruit then --This right here is what I assume the problem is
				print("Fruit") --It keeps printing "Fruit"
				dialog.InitialPrompt = "I know where the DevilFruit is"
				if chosen_spawner.Name == "GrassFruitSpawner" then
			dialogChoice.ResponseDialog = "Grassy Island!"
				elseif chosen_spawner.Name == "GrimmyFruitSpawner" then
			dialogChoice.ResponseDialog = "Land of the dead..."
				elseif chosen_spawner.Name == "InvisibleFruitSpawner" then
			dialogChoice.ResponseDialog = "You cannot see me"
				elseif chosen_spawner.Name == "PinkFruitSpawner" then
			dialogChoice.ResponseDialog = "You are dreaming..."
				elseif chosen_spawner.Name == "SandFruitSpawner" then
			dialogChoice.ResponseDialog = "Very hot tbh"
				elseif chosen_spawner.Name == "SnowFruitSpawner" then
			dialogChoice.ResponseDialog = "Very cold tbh"
				end
	elseif not foundFruit then
		print("Fruit is not here yet")
		dialog.InitialPrompt = "There is currently no fruit.. still looking for one"
		dialogChoice.ResponseDialog = "GET OUT OF MY FACE N00B IM STILL LOOKING"
	end
end


return DialogChoiceModule

Even if I delete or collect the tool it still prints fruit. And if you read the script you would know that if tool parent isn’t chosen_spawner then fruitFound = false. Which means the dialog part can’t work. Also if there is anything else wrong with this script feel free to correct. I am tired of trying to fix this myself and I decided to just let it out now lol.

The script goes through all of your spawners, finds the one with the tool, declares the tool as the child of the spawner, then asks if the tool is the child of the spawner;

You declare “tool” as the child of chosen_spawner, then immediately test whether tool.parent == chosen_spawner, which will always be true because nothing changed between there. when .TrackFruit is run, there is no possible way that foundFruit can become false because if tool.Parent is NOT chosen_spawner, then the statement:
if spawner:FindFirstChildWhichIsA("Tool") and chosen_spawner == nil then
will be false and the rest of the code will not run.

Possible solution:

function DialogChoiceModule.TrackFruit()
	for _,spawner in pairs(game.Workspace.Spawners:GetChildren()) do --Get table of spawners
		if string.match(spawner.Name,"FruitSpawner") then
			tool = --DEFINE YOUR TOOL HERE--
			if spawner:FindFirstChildWhichIsA("Tool") and chosen_spawner == nil then
				print("Spawner with tool found!")
				chosen_spawner = spawner
					if tool.Parent == chosen_spawner then
						foundFruit = true
				    end
            elseif tool.Parent ~= chosen_spawner then
				   foundFruit = false
				   chosen_spawner = nil 
            end
		end
	end
end

Edit:

Changed where “tool” is defined, you must define it some other way (whether it’s in the player’s inventory, or outside of the module script and have it as an argument for .Trackfruit()
… ie. .TrackFruit(tool)

1 Like

I am actually happy rn. Using your side on the problem, since I couldn’t set the tool to anything outisde the module script, I just worked around it. So basiclly using your idea and mine I was able to solve this by changing some things based on your idea. Thank you, if it wasn’t for you I would still be stressing over this problem.
Here is the changed script btw:

local DialogChoiceModule = {} 

local chosen_spawner 
local tool
local foundFruit 

function DialogChoiceModule.TrackFruit()
	for _,spawner in pairs(game.Workspace.Spawners:GetChildren()) do --Get table of spawners
		if string.match(spawner.Name,"FruitSpawner") then
			if spawner:FindFirstChildWhichIsA("Tool") and chosen_spawner == nil then
				tool = spawner:FindFirstChildWhichIsA("Tool")
				print("Spawner with tool found!")
				chosen_spawner = spawner
				foundFruit = true
			elseif chosen_spawner ~= nil and not chosen_spawner:FindFirstChildWhichIsA("Tool") then
				foundFruit = false
				chosen_spawner = nil 
				print("No tool")
			end
		end
	end
end

function DialogChoiceModule.DialogChoice(npc)
	local dialog = npc.Head:FindFirstChild("Dialog")
	local dialogChoice = dialog:FindFirstChild("DialogChoice")
	
			if foundFruit then
				wait(1)
				print("Fruit")
				dialog.InitialPrompt = "I know where the DevilFruit is"
				if chosen_spawner.Name == "GrassFruitSpawner" then
			dialogChoice.ResponseDialog = "Grassy Island!"
				elseif chosen_spawner.Name == "GrimmyFruitSpawner" then
			dialogChoice.ResponseDialog = "Land of the dead..."
				elseif chosen_spawner.Name == "InvisibleFruitSpawner" then
			dialogChoice.ResponseDialog = "You cannot see me"
				elseif chosen_spawner.Name == "PinkFruitSpawner" then
			dialogChoice.ResponseDialog = "You are dreaming..."
				elseif chosen_spawner.Name == "SandFruitSpawner" then
			dialogChoice.ResponseDialog = "Very hot tbh"
				elseif chosen_spawner.Name == "SnowFruitSpawner" then
			dialogChoice.ResponseDialog = "Very cold tbh"
				end
	elseif not foundFruit then
		print("Fruit is not here yet")
		dialog.InitialPrompt = "There is currently no fruit.. still looking for one"
		dialogChoice.ResponseDialog = "GET OUT OF MY FACE N00B IM STILL LOOKING"
	end
end

return DialogChoiceModule
1 Like