Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

help with voice influenced npc?

I want to make a script where if you are a certain distance away from an NPC, like a maximum of 10 studs, and you say something like hi, it will say hi back. does anyone know how?

I know already how to do the chat

game:GetService("Chat"):Chat(script.Parent.Head, "Hello.", Enum.ChatColor.White)

and i have a door script to like recognize if you chatted

function onChatted(msg, recipient, speaker)

local source = string.lower(speaker.Name)
msg = string.lower(msg)

if (msg == "hi") then

2 answers

✓ Accepted answer
-- First comes our global variables.
local npc = script.Parent

-- Services
local chatService = game:GetService("Chat")
local plrs = game:GetService("Players") -- Define our player service.

-- Variables
local messages = 
    {
        ["hi"] = "Hello Player!",
    }

--- Functions
function chat(part,msg,color)
    chatService:Chat(part,msg,color or Enum.ChatColor.White)
end

function checkMessage(msg)
    msg = string.lower(msg) -- lowercase our message

    local response = messages[msg]

    if response then
        chat(npc.Head,response)
    end
end

function playerAdded(plr)   -- Define our function for when players join.
    plr.Chatted:Connect(function(message,reciever)
        if plr.Character then       -- Check if the player has a character
            if not reciever then    -- Make sure it isn't a private message.
                local plrPos= plr.Character:GetPrimaryPartCFrame().p
                local npcPos = npc:GetPrimaryPartCFrame().p
                local distance = (plrPos-npcPos).magnitude -- Define the distance from the character to our NPC.

                if distance < 10 then
			wait(1)
                    checkMessage(message)
                end
            end
        end
    end)
end

-- Event listeners
plrs.PlayerAdded:Connect(playerAdded)

Hello Xeilot,

First, let's define a chat function for you to use:

local chatService = game:GetService("Chat")

function chat(part,msg,color)
	chatService:Chat(part,msg,color or Enum.ChatColor.White)
end

This allows you to make any part you want chat. We define the chat service outside of the function so we don't have to get it every time we use the function.

After that, we'll create a checkMessage function to check valid messages, and have the NPC reply.

local messages = 
	{
		["player message here"] = "NPC Response here",
	}

function checkMessage(msg)
	msg = string.lower(msg) -- lowercase our message
	
	local response = messages[msg]

	if response then
		chat(npc.Head,response)
	end
end

Next, we need to check for player chats. This one is a bit more tricky, but overall pretty easy.

local plrs = game:GetService("Players") -- Define our player service.

function playerAdded(plr)	-- Define our function for when players join.
	
end

plrs.PlayerAdded:Connect(playerAdded)

Inside that function we can check when a player has chatted.

local plrs = game:GetService("Players") -- Define our player service.

function playerAdded(plr)	-- Define our function for when players join.
	plr.Chatted:Connect(function(message,reciever)
		if plr.Character then		-- Check if the player has a character
			if not reciever then	-- Make sure it isn't a private message.
				local distance = (plr.Character:GetPrimaryPartCFrame().p-npc:GetPrimaryPartCFrame.p) -- Define the distance from the character to our NPC.
				
				if distance < 10 then
					checkMessage(message)
				end
			end
		end
	end
end

plrs.PlayerAdded:Connect(playerAdded)

Now that we have those three components, let's define our NPC.

local npc = script.Parent -- Change this if your script is in a different place.

We're done! Now we can combine it into one script:

-- First comes our global variables.

-- Services
local chatService = game:GetService("Chat")
local plrs = game:GetService("Players") -- Define our player service.

-- Variables
local messages = 
	{
		["player message here"] = "NPC Response here",
	}

--- Functions
function chat(part,msg,color)
	chatService:Chat(part,msg,color or Enum.ChatColor.White)
end

function checkMessage(msg)
	msg = string.lower(msg) -- lowercase our message
	
	local response = messages[msg]

	if response then
		chat(npc.Head,response)
	end
end

function playerAdded(plr)	-- Define our function for when players join.
	plr.Chatted:Connect(function(message,reciever)
		if plr.Character then		-- Check if the player has a character
			if not reciever then	-- Make sure it isn't a private message.
				local plrPos= plr.Character:GetPrimaryPartCFrame().p
				local npcPos = npc:GetPrimaryPartCFrame().p
				local distance = (plrPos-npcPos).magnitude -- Define the distance from the character to our NPC.
				
				if distance < 10 then
					checkMessage(message)
				end
			end
		end
	end
end

-- Event listeners
plrs.PlayerAdded:Connect(playerAdded)

And we're done! The NPC now can detect when a player chats, check the distance between it and the player, and then reply!

If you've got any questions, feel free to ask me.

2 comments

0

When I try putting that in, it says the last end has a red line under it

0

and it doesn't seem to work

This discussion is preserved as it was. New answers, comments, and votes are closed.