For i, v in pairs loop failing to run

Hello, I’m creating a game that uses an NPC that gives you different dialogue depending on how many times you’ve talked to him, for the first part of the script or the first time talking/interacting with him, it works fine, however when you do it the second time the for i, v in pairs function doesn’t even run and is just skipped entirely, with only 1 error occuring that says: "Frame is not a valid member of ScreenGui “ScreenGui1”. The bool values in my case : FirstTalk And tutorialfinished are both set to false at the start and function perfectly.

Here is the script:

 local debounce = false
local cooldown = 5

local Messages = {"Yo dude", "You wanna collect taxes or something?", "Use the car outside and go to houses that haven't yet payed their taxes", "Good Luck and come back to me after your first job."}
local Messages2 = {"lol good job bud", "You can use the shop in the top right of your screen to buy cool new vehichles and stuff with tax bucks", "Now go get us more tax dollars and stuff", "oh btw sometimes we get robbed and stuff."}
local Reply1 = {"idc", "go do it anyways"}
local Reply2 = {"alr gl"}
game:GetService("ReplicatedStorage").Chats.Joe1.OnClientEvent:Connect(function(hit)
	print(debounce)
	if debounce == false then
		debounce = true

		local GUI = script.Parent.ScreenGui1:Clone()
		local Player = hit

		local Rig = game.Workspace.NPCJoe:Clone()

		GUI.Parent = script.Parent
		GUI.Frame.NPC.Text = "Joe"
		GUI.Enabled = true

		script.Parent.Parent.Character.Humanoid.WalkSpeed = 0
		Rig.Parent = GUI.Frame.ViewportFrame
		if script.Parent.ScreenGui1.FirstTalk.Value == false then
		for i, v in pairs(Messages) do
			for i = 1, string.len(v) do wait(.025)
				script.Parent.NPC_70:Play()
				GUI.Frame.Dialogue.Text = string.sub(v, 1, i)
			end
			wait(1)
		end
		GUI.Frame.Yes.Visible = true
		GUI.Frame.No.Visible = true
		elseif script.Parent.ScreenGui1.FirstTalk.Value == true and script.Parent.ScreenGui1.TutorialFinished.Value == false then
			print("cago")
			for i, v in pairs(Messages2) do
				for i = 1, string.len(v) do wait(.025)
					script.Parent.NPC_70:Play()
					script.Parent.ScreenGui1.Frame.Dialogue.Text = string.sub(v, 1, i)
				end
				wait(1)
				script.Parent.Parent.Character.Humanoid.WalkSpeed = 16
				GUI:Destroy()
				end
		end
		
		GUI.Frame.Yes.MouseButton1Down:Connect(function(plr)
			GUI.Frame.Yes.Visible = false
			GUI.Frame.No.Visible = false
				for i, v in pairs(Reply2) do
					for i = 1, string.len(v) do wait(.025)
						script.Parent.NPC_70:Play()
						GUI.Frame.Dialogue.Text = string.sub(v, 1, i)
					end
					wait(1)
				end
				script.Parent.Parent.Character.Humanoid.WalkSpeed = 16
				debounce = false
				GUI:Destroy()
				script.Parent.ScreenGui1.FirstTalk.Value = true
			end)

		GUI.Frame.No.MouseButton1Down:Connect(function(plr)
			GUI.Frame.Yes.Visible = false
			GUI.Frame.No.Visible = false
			for i, v in pairs(Reply1) do
				for i = 1, string.len(v) do wait(.025)
					script.Parent.NPC_70:Play()
					GUI.Frame.Dialogue.Text = string.sub(v, 1, i)
				end
				wait(1)
			end
			script.Parent.Parent.Character.Humanoid.WalkSpeed = 16
			debounce = false
			GUI:Destroy()
			script.Parent.ScreenGui1.FirstTalk.Value = true
		end)

	end
end)

image

Video : https://streamable.com/bkti03

So far I’ve looked through the devforum to find similar experiences to mine but haven’t got anywhere.
I’ve tried using things like :WaitForChild and other ways to path to my frame but it doesn’t seem to be working.

If anyone has had something similar happen to them and/or can help I would be greatly appreciative

Firstly, which line does the error occur on?
Secondly, try moving the ScreenGui template (the one that is cloned from) to a different place since it may mistake that and the cloned one with each other

Most likely it’s the GUI:Destroy() found here that’s causing this problem

Unrelated to your question, but if you’re looping over a list, you should use ipairs instead of pairs, for performance.

Thank you! This was one of the few issues in my code and it worked when I changed this.

Thank you so much, I moved the screengui into my npc and had it clone into playergui and then it worked.

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