Need help with NPC chat system

  1. What do you want to achieve?
    Make a NPC chat system with GUIs
  2. What is the issue?
    The chat is all mixed up. The NPC says stuff the player is supposed to say. And sometimes they both say the same stuff.
  3. What solutions have you tried so far?
    I worked on this for over 3 hours.

HERE IS MY CODE

NPC CODE


local button = script.Parent
local plr = game.Players.LocalPlayer
local function onButtonActivated()
	plr.PlayerGui.ScreenGui.GameFrame.ReplyLabel1.Visible = true
	
	plr.PlayerGui.ScreenGui.GameFrame.ReplyLabel1.Text = button.CurrentQuestion.Value.Value
	
	if button.CurrentReply:FindFirstChild("Question") then
		button.CurrentText.Value = button.CurrentReply.Question.Value
		print(button.CurrentText.Value)
		
	
	
	
end
end
button.Activated:Connect(onButtonActivated)

THE CODE INSIDE OF THE SCREEN GUI

local button = script.Parent

local function onButtonActivated()
	script.Parent.Visible = false
	local Dialog = script.Parent.Parent.Parent.Parent.SurfaceGui.Dialog
	Dialog.Text = Dialog.CurrentReply.Value.Value
	
	if Dialog.CurrentReply.Value:FindFirstChild("Question") then
		Dialog.CurrentReply.Value = Dialog.CurrentReply.Value.Question
		Dialog.CurrentReply.Value.Used.Value = true
		
		if Dialog.CurrentQuestion.Value:FindFirstChild("Reply") then
			Dialog.CurrentQuestion.Value = script.Parent.Parent.Parent.Parent.SurfaceGui.Dialog.CurrentQuestion.Value.Reply
			Dialog.CurrentQuestion.Value.Used.Value = true
end
	end
	end
button.Activated:Connect(onButtonActivated)

PLEASE HELP ME

I find this code hard to read. I think it’s odd that you are messing with UIs from the server side… (Personal Opinion)

If a chat dialog system is not a core aspect to your game, then may I suggest that you use roblox dialog system to make things easier and to contribute to your workflow.

Wiki On NPC Dialogs - (Might make you life easier.)

Ok, I will try that. But I still would like my scripts to work.

You have used too many “long ways” of coding what you are trying to make. I would suggest studying other peoples code to find common practices. Try watching some YouTube videos and read the wiki a lot.

To give you an example, instead using the infinite lines of Parent.Parent.Parent.Parent… you could took a more simpler approach and used :FindFirstAncestor().

Or maybe properly handling the server to client boundaries by using RemoteEvents and RemoteFunctions.

But you have presented your issues properly and I appreciate that… but this code is hard to understand what you are attempting to do. Maybe someone with more patients will come along and help you. Keep trying and don’t loose motivation.