Why Is This NPC Chat Script Not Working?

I’m trying to use an NPC Chatting system, but neither the X to skip and finish the text or the go to next line is working (ie. it stays on the same line and does not switch), does anyone have an idea on how to fix this?

Here is the script:


--// Services


local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")





--// Main Variables

local DetectedNPC = nil
local Detected = false
local Chatting = false
local Skip = false
local Exit = false






--// Player


local Player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

local Gui = script.Parent
local Sounds = Gui.Sounds
local PromptLabel = Gui.PromptLabel
local LineLabel = Gui.LineLabel





--// Character


local Character = Player.Character or Player.CharacterAdded:Wait()
local CharHMR = Character:WaitForChild("HumanoidRootPart")





--// NPC
local NPCS = game.Workspace:WaitForChild("NPCS")
--// Functions


UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.X then
		if Chatting == true then
			Skip = true
			Sounds.Click:Play()
		end
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Z then
		if Chatting == true then
			Exit = true
			Sounds.Click:Play()
		end
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		
		
		if Detected == true then
			local Lines = DetectedNPC:FindFirstChild("Lines")
			
			if Lines then
				Sounds.Click:Play()
				
				Chatting = true
				Detected = false
				
				LineLabel.Text = " "
				
				PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
				LineLabel.Visible = true
				
				wait(0.5)
				
				for i, Line in pairs(Lines:GetChildren()) do
					local Text = Line.Value
					
					for i = 1, #Text do
						LineLabel.Text = string.sub(Text, 1, i)
						Sounds.Talk:Play()
						if Skip == true then
							LineLabel.Text = Text
							Skip = false
							break
						end
						if Exit == true then
							break
						end
						wait(0.07)
					end
					if Exit == true then
						Exit = false
						break
					end
					repeat wait() until Skip == true or Exit == true
					Skip = false
				end
				
				Exit = false
				Skip = false
				
				PromptLabel:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
				LineLabel.Visible = true
				
				wait(0.5)
				
				Chatting = false
				Detected = false
			end
		end
	end
end)


--// Main Loop

RunService.RenderStepped:Connect(function()
	
	
	
	
	if Detected == false and Chatting == false then
		for i, NPC in pairs(NPCS:GetChildren()) do
			local Humanoid = NPC:FindFirstChild("Humanoid")
			local HMR = NPC:FindFirstChild("HumanoidRootPart")
			
			if Humanoid and HMR then
				if (HMR.Position - CharHMR.Position).magnitude < 15 then
					Detected = true
					DetectedNPC = NPC
					PromptLabel:TweenSize(UDim2.new(0, 60, 0, 60), "In", "Linear", 0.2)
					print(DetectedNPC.Name)
				end
			end
			
		end
	end
	
	if Detected == true and Chatting == false then
		local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
		local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
			
		if Humanoid and HMR then
			if (HMR.Position - CharHMR.Position).magnitude > 3 then
				Detected = false
				DetectedNPC = nil
				LineLabel.Visible = false
				print("No Longer Detected NPC")
			else
				local WTSP = Camera:WorldToScreenPoint(HMR.Position)
				PromptLabel.Position = UDim2.new(0, WTSP.X, 0, WTSP.Y)
			end
		end
	end
	
	if Chatting == true then
		local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
		local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
			
		if Humanoid and HMR then
			Camera.CameraType = Enum.CameraType.Scriptable
			Camera.CFrame = Camera.CFrame:Lerp(HMR.CFrame * CFrame.new(-4, 4, -7) * CFrame.fromOrientation(math.rad(-20), math.rad(215), 0), 0.07)
		end
	else
		Camera.CameraType = Enum.CameraType.Custom
	end
	
	
end)

One thing you can try is to add print statements inside each of the InputBegan functions to check if they are being called when you press the corresponding key. For example, you can add this inside the first InputBegan function:

print(“X key pressed”)

Then, when you run the script and press the X key, you should see the “X key pressed” message in the output. If you don’t see the message, then there might be an issue with the event listener not being set up correctly.

If the event listeners are being triggered properly, then the issue might be with how the script is handling the Skip and Exit variables. You can try adding more print statements throughout the script to see if these variables are being set to true when you press the corresponding key, and if they are being reset properly when the conversation ends.

Another thing to check is if the LineLabel is being updated properly. You can add print statements inside the for loop that updates the Line.

Sure, I’ll try this right now.

No matter what I do, the X being clicked does not register, I’m not sure why though.

I fixed it! Not sure why, but for some reason the key being X broke it. When I switched it to Q, it fixed itself. Weird.

2 Likes

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