For i,v pairs loop help

Alright so I have a loop and it’s looping through everything I want but like problem is that its doing it two times its kind of hard to explain heres the code

function Checks:Click()
	self.MouseButton1Click:Connect(function()
		for index,response in pairs(Dialog.Lines.NPCLines[self.Name]) do
			for i,start in pairs(Dialog.Lines.PlayerLines[self.Name]) do
				if (index == i) then
					local responsetext = self.Parent.Parent.Parent.Dialog.response
					Animated.typeWrite(responsetext, response)
				end
			end
		end
	end)
end
1 Like

So if it is looping twice, you should create an if statement. This if statement should check when the text is where it should be, and if it returns nil, keep going, but if it gets the text you want it to, you can stop the loop so it doesn’t get to the second one.

1 Like

Alright let me try something like this thanks

5 Likes

could you explain in more detail it doesnt seem to be working and im not sure how to implement in the code above, i got it to stop doing both now but the problem is it only does it with the 1st index and if i press the button with another completely different index it works but it prints the 1st indexes value

function Checks:Click()
	self.MouseButton1Click:Connect(function()
		for index,response in pairs(Dialog.Lines.NPCLines[self.Name]) do
			for i,start in pairs(Dialog.Lines.PlayerLines[self.Name]) do
				if (index == i) then
					local responsetext = self.Parent.Parent.Parent.Dialog.response
					Animated.typeWrite(responsetext, response)
				else
					break
				end
			end
		end
	end)
end

1 Like

Based upon what your code looks like, I would say you would need to do something like this:

function Checks:Click()
	self.MouseButton1Click:Connect(function()
		for index,response in pairs(Dialog.Lines.NPCLines[self.Name]) do
			for i,start in pairs(Dialog.Lines.PlayerLines[self.Name]) do
				if (index == i) then
					local responsetext = self.Parent.Parent.Parent.Dialog.response
					Animated.typeWrite(responsetext, response)
                    if Dialog.Lines.NPCLines ( .Value/.Text ) == responsetext then
                    --Break loop
                    else
                    --It should continue going
                    end
				end
			end
		end
	end)
end

Again, this part I added in is not fully complete as I don’t know where some of this is leading/going towards, but that is what my idea of where it is going towards means.

2 Likes

You could just add a simple ‘found’ type variable.

function Checks:Click()
        local found
	self.MouseButton1Click:Connect(function()
		for index,response in pairs(Dialog.Lines.NPCLines[self.Name]) do
			for i,start in pairs(Dialog.Lines.PlayerLines[self.Name]) do
				if (index == i) and found == nil then
                                        found = true
					local responsetext = self.Parent.Parent.Parent.Dialog.response
					Animated.typeWrite(responsetext, response)
				end
			end
		end
	end)
end

And if that doesn’t fix your issue then it’s probably something to do with the module.

1 Like

The module is literally tables so I doubt its the module, also I tried your method and it’s working but like it doesnt assign the response with the right index