NPC Chat Dialogue Repeating Messages

  1. What do you want to achieve?

NPC Chat Dialogue to do each message individually for each character using the correct settings.

  1. What is the issue?

https://gyazo.com/0facabf3e8a3568518218270733b36e0

For some reason the chat is getting repeated, and It broke after switching from ALL npcs to saying the same thing to INDIVIDUAL npcs saying a different thing.

  1. What solutions have you tried so far?

Taking a look at my code I tried fixing the local lastMessage and seeing why it is repeating the 0, and staying on the first message set.

Everything works, but the chat is getting repeated.


What I think is the problem :

npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.MessagesTwin/#npcs)

I best believe that the math.floor(#SETTINGS.MessagesTwin/#npcs) is the problem. Reason being its checking all of the NPCS instead of just the one npc, but I tried changing #npcs to #npc and it didn’t make a difference.

Here is the full code -

--[[
	@author TwinPlayzDev_YT
	@since 9/5/2021
	This script manages the NPC Talk. 

	youtube.com/c/TwinPlayz_YT
--]]

--[[ SETTINGS ]]--
local SETTINGS = {
	MaxInteractionDistance = 6,
	Messages = {
	    "Hola Soy Dora! Welcome to Dora's Hangout, created by TwinPlayz!",
	    "If you haven't already, you should checkout the emotes, commands, and maps!",
	    "Join our communications server for updates!",
		"Join Dora's Fam today! You can get /korblox, and /headless!"
	},
	MessagesTwin = {
		"Hello there! Want to make a game like this? Checkout TwinPlayz on Youtube! Go Subscribe!",
		"You can learn on my channel how to script, build, and use Roblox Studio!",
		"Start developing today! You won't regret it! -> TWINPLAYZ"
	},
	MessagesGun = {
		"Hey there! Im GunSlaya's Clone nice to meet you.",
		"Have you considered making a video in our game yet? If not you should!",
		"PSSSS : One of my favorite commands, is /bloxikin you REALLY should try it!!!!"
	},
	MessagesDiego = {
		"Hey there! Im Diego's Clone nice to meet you!",
		"I really wonder what could be down this ally.... go check it out!!!",
		"PSSSS : Join our group, and you can get /korblox, and /headless!"
	},
	MessagesSwiper = {
		"Hey there! Im Swipers Clone nice to meet you!",
		"Welcome to the shop! Here you can buy gears to use in your dances!",
		"PSSSS : If you buy the VIP Gamepass, you can get 2X Cash!"
	},
	MessageDelay = 10
}

local ChatService = game:GetService("Chat");

local npcs = {
	{
		Char = workspace.NPCLookAt.TwinPlayzDev_YT,
		lastMessage = 0,
		lastMessageTime = 0,
	},
	{
		Char = workspace.NPCLookAt.Junewuuu,
		lastMessage = 0,
		lastMessageTime = 0,
	},
	{
		Char = workspace.NPCLookAt.GunSlaya,
		lastMessage = 0,
		lastMessageTime = 0,
	},
	{
		Char = workspace.NPCLookAt.Swiper,
		lastMessage = 0,
		lastMessageTime = 0,
	},
	{
		Char = workspace.NPCLookAt.Diego,
		lastMessage = 0,
		lastMessageTime = 0,
	}
}

local target = (game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()):WaitForChild("Head")

--[[ CHARACTER ADDED ]]--

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
	target = char:WaitForChild("Head");
end)

--[[ MESSAGE ]]--
while wait(1) do
	for i, npc in pairs(npcs) do
		local inRange = (target.Position - npc.Char:WaitForChild("HumanoidRootPart").Position).magnitude <= SETTINGS.MaxInteractionDistance
		if inRange and tick() - npc.lastMessageTime >= SETTINGS.MessageDelay then
			
			npc.lastMessageTime = tick()
			
			if npc.Char.Name == "TwinPlayzDev_YT" then
				ChatService:Chat(npc.Char:WaitForChild("Head"), SETTINGS.MessagesTwin[npc.lastMessage%#SETTINGS.MessagesTwin+1], "White")
				npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.MessagesTwin/#npcs)
			elseif npc.Char.Name == "Junewuuu" then
				npc.lastMessageTime = tick()
				ChatService:Chat(npc.Char:WaitForChild("Head"), SETTINGS.Messages[npc.lastMessage%#SETTINGS.Messages+1], "White")
				npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.Messages/#npcs)
			elseif npc.Char.Name == "GunSlaya" then
				npc.lastMessageTime = tick()
				ChatService:Chat(npc.Char:WaitForChild("Head"), SETTINGS.MessagesGun[npc.lastMessage%#SETTINGS.MessagesGun+1], "White")
				npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.MessagesGun/#npcs)
			elseif npc.Char.Name == "Swiper" then
				npc.lastMessageTime = tick()
				ChatService:Chat(npc.Char:WaitForChild("Head"), SETTINGS.MessagesSwiper[npc.lastMessage%#SETTINGS.MessagesSwiper+1], "White")
				npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.MessagesSwiper/#npcs)
			elseif npc.Char.Name == "Diego" then
				npc.lastMessageTime = tick()
				ChatService:Chat(npc.Char:WaitForChild("Head"), SETTINGS.MessagesDiego[npc.lastMessage%#SETTINGS.MessagesDiego+1], "White")
			    npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.MessagesDiego/#npcs)
			end
			
			--npc.lastMessage = npc.lastMessage + math.floor(#SETTINGS.Messages/#npcs)
			
		elseif not inRange then
			npc.lastMessageTime = 0
		end
	end
end

What could be the problem here, it’s really confusing me because it was working at first but now broke because I made the NPCS individual?

Hi, as you say, it seems that #SETTINGS.MessagesTwin/#npcs gives 3/5, when you really probably want to add just 1 to it.

Here you could use the modulo % sign. When you do a % b you substract b as many times from a as possible and get the remainder. So in this case
npc.lastMessage = npc.lastMessage % #SETTINGS.MessagesTwin + 1

Since lua tables start at index 1, it’s probably neater to initialize it like that. By doing the modulo first and only adding 1 afterwards, we can make sure that the result is never 0. (because a%(x*a) = 0)

Also, one note about your code: When you have many repeating sentences copy pasted in a row, its a strong signal that there may be a much cleaner solution using a for-loop or a function. The repeating lines can be put in there, and fixing a bug like this one also becomes much easier!

1 Like