Dialog system problem

hi i am making a custom npc chat system and i am using run service to put e button in the npc but the text that its writing is from the previous npc
i used print() to try and figure what is the problem and from what it printed
its switching form previous humonoidRootPart to HumanoidRootPart of the current npc
here is the script:

--vars
local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local tweens = game:GetService("TweenService")
local MAIN_LANG = "en"
local camera = workspace.CurrentCamera
local camMODE = Enum.CameraType.Scriptable
local running = false

local AwFrame = script.Parent.Parent.ScrollingFrame

local DefName = "D1"


--end of vars
--tables and stuff

local currentNPC = nil

--calculations and stuff


--

repeat wait() until player.Character

local function returnA(a)	
	local stringr = string.format(a)
	return stringr
end

local function CustomLetpFunc(CF,Obj,AlphaTIME)
	if Obj then
		if Obj:IsA("BasePart") then
			Obj.CFrame = Obj.CFrame:Lerp(CF,AlphaTIME)
		else
			warn("Erro#1:Obj Not A Object")
		end	
	else
		warn("Error#2:Obj Not Valid")
	end
end

--main Part

local function Write(Text: string,ui: Instance,NPC: Instance)
	if string.len(Text) >= 1 and ui ~= nil and NPC ~= nil then
		local s,e = pcall(function()
			for i = 1,#Text do
				ui.Text = string.sub(Text,1,i)
				wait(#Text/1000)
			end
		end)
	else
		warn("Error#3:line 52 something is nill")
	end
end
local eButton = game.ReplicatedStorage.E

				local button = eButton:Clone()
			

game:GetService("RunService").RenderStepped:Connect(function()

	for i,npcs in pairs(workspace.Ncs:GetChildren()) do
		local CharHRP = npcs:FindFirstChild("HumanoidRootPart");
		local HMRP = player.Character.HumanoidRootPart 
		local mag = (HMRP.Position - CharHRP.Position).magnitude
		if (mag) < 15 and running == false then
			currentNPC = npcs
			CharHRP = currentNPC:FindFirstChild("HumanoidRootPart")
			button.Parent = CharHRP
			script.Parent.Parent.n.Text = currentNPC.Name
			uis.InputBegan:connect(function(c)
				print(currentNPC.Name,CharHRP.Parent.Name)
				if c.KeyCode == Enum.KeyCode.E and running == false then
					wait(0.5)
					HMRP.Anchored = true
					camera.CameraType = camMODE
					camera.CFrame = camera.CFrame:Lerp(CharHRP.CFrame * CFrame.new(-3, 3, -5) * CFrame.fromOrientation(math.rad(-10), math.rad(215),0),.5)
					AwFrame.Parent.Visible = true
					running = true
					local text = currentNPC.Talk.D1.Value
					local textOBJ = currentNPC.Talk.D1
					local C1 = {textOBJ.Coise1.Value};
					local C2 = {textOBJ.Coise2.Value};
					local C1o = {textOBJ.Coise1};
					local C20 = {textOBJ.Coise2};
					Write(text,script.Parent,0.09,currentNPC);
					wait(0.1);
					AwFrame.Visible = true;
					local one = AwFrame.One
					local two = AwFrame.Two
					one.Text = C1[1]
					two.Text = C2[1]
					one.MouseButton1Click:connect(function()
						AwFrame.Visible = false
						wait(0.1)
						Write(C1o[1].A.Value,script.Parent,0.05,currentNPC)
						wait(1)
						camera.CameraType = Enum.CameraType.Custom
						AwFrame.Parent.Visible = false
						HMRP.Anchored = false
						currentNPC = nil
						wait(3)
						running = false
					end)
					two.MouseButton1Click:connect(function()
						AwFrame.Visible = false
						wait(0.1)
						Write(C20[1].A.Value,script.Parent,0.05,currentNPC)
						wait(1)
						camera.CameraType = Enum.CameraType.Custom
						AwFrame.Parent.Visible = false
						HMRP.Anchored = false
						currentNPC = nil
						wait(3)
						running = false
					end)
				end
			end)
		end	
	end
end)```

i cant figure this stuff out i have tried to set the npc to nill but that didnt work

if someone can help thank you
1 Like