How to switch from MouseButtonClick to ProximityPrompt in this script?

I tried using pairs instead of ipairs and as you said, it made the dialogue show the lines not in order.

I tried adding the print(lines) here as you asked me to:

And this is what was printed in OutPut (for the first NPC, the only one that has 9 lines, which is the only one I’m facing an issue with so far):
(I noticed that at some point the value of line3 was void for some reason?)

And here is what appeared in OutPut when I talked to another random NPC (this NPC has 3 lines, but its dialogue has no issues and it is working perfectly fine like the other NPC’s):

I also tried to play test again using pairs instead of ipairs and somehow it worked, but then I exited and tried to play test again and the lines got all messy once again. But the thing I noticed is that when using ipairs, for the first NPC only the first 2 lines show and then the dialogue ends, meanwhile when using pairs, the dialogue shows the 9 lines but the issue is that they are not in the correct order.

Finally got a working code! Thank you so much to @Dev_Sie for fixing it <3
Here is the code if anyone ever needs it:

local mod = require(script.ModuleScript)

local player = game:GetService("Players").LocalPlayer

local npcs = workspace:WaitForChild("Npcs")

local gui = script.Parent
local MainFrame = gui.MainFrame
local dialogue = MainFrame.Dialouge

local sound = script.Sound

canTalk = true

local Lines = {"Line1","Line2","Line3","Line4","Line5","Line6","Line7","Line8","Line9","Line10","Line11","Line12","Line13","Line14","Line15"}

local function proximityPromptTriggered(npc)
	local DialogueOptions = npc:FindFirstChild("DialogueOptions")
	if canTalk == true and DialogueOptions then -- makes sure npc has DialogueOptions
		canTalk = false

		local char = player.Character --[[ gets current Character, saving it in variable outside this function will 
		cause it to store the wrong Character whenever they respawn ]]

		local humanoid = char:WaitForChild("Humanoid")
		humanoid.WalkSpeed = 0

		MainFrame.Visible = true
		MainFrame.Title.Text = npc.Name

		local lines = {}

		for _, v in DialogueOptions:GetChildren() do
			if v:IsA("StringValue") and v.Name:sub(0, 4) == "Line" and v.Name:match("%d+") then --checks if the instance is a StringValue, has the word line at the beginning, and has numbers in it 
				table.insert(lines, v.Name:match("%d+"), v.Value) -- insert the value of the line (string) into the lines table, index is the number in the name of the value	
			end
		end
		
		local newLines = npc.DialogueOptions:GetChildren()
		if #newLines > 5 then
			table.clear(lines)
			for i,v in pairs(Lines) do
				local line = npc.DialogueOptions:FindFirstChild(v)
				if not line then continue end
				table.insert(lines,line.Value)
			end
		end
        print(lines)
		for _, line in lines do
			mod.typewrite(dialogue, line, .02, sound)
			task.wait(4) --faster and more accurate than wait(n), see https://devforum.roblox.com/t/task-library-now-available/1387845
		end

		MainFrame.Visible = false
		MainFrame.Dialouge.Text = " "
		humanoid.WalkSpeed = 16
		canTalk = true
	end
end

for _, npc in workspace.Npcs:GetChildren() do
	local proximityPrompt = npc:FindFirstChildWhichIsA("ProximityPrompt")
	if proximityPrompt then
		proximityPrompt.Triggered:Connect(function()
			proximityPromptTriggered(npc)
		end)
	end
end

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