If the player chats someone to the nearby NPC near him then the NPC chats back one of the chats like can I have a pizza or can I have a part, This is for my Cooking Game and I need it for the Summer Update, I mostly need help with this one, Here’s the script that I’m trying to work, can someone send me a new script or help me on this, I need before July 15, 2024 so here’s the code:
local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local modelRoot = script.Parent.HumanoidRootPart
local NPC = workspace:FindFirstChild("Franklin Walking From Part To Part")
local distance = (NPC.PrimaryPart.Position-game.Players.LocalPlayer.Character.PrimaryPart.Position).Magnitude
local responses = {
["Hello, What would you like to order?"] = {
"Can i get a Pizza",
"Can i get a Pizza",
""
},
}
function getClosestPlayer()
local closest_player, closest_distance = nil, 200
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and player ~= NPC then
local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < closest_distance then
closest_player = player
closest_distance = distance
end
end
end
return closest_player, closest_distance
end
local player, distance = getClosestPlayer()
Players.PlayerAdded:Connect(function(player)
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
local playerRoot = char:WaitForChild("HumanoidRootPart")
player.Chatted:Connect(function(msg)
local message = responses[msg]
local distance = (playerRoot.Position - modelRoot.Position).Magnitude
if message and script.Parent:FindFirstChild("Humanoid").Sit == true then
if player and distance > 10 then
task.wait(1)
local getRandomResponse = message[math.random(1,#message)]
Chat:Chat(script.Parent.Head, getRandomResponse, Enum.ChatColor.Blue)
else
Chat:Chat(script.Parent.Head, "Get out of my sight!", Enum.ChatColor.Blue)
end
end
end)
end)
end)
heres the NPC in the Blue!
3 Likes
If you need them to have the same names for some reason, you could differentiate them with attributes and get an NPC with like a
local players = game:GetService("Players")
local getNpcByAttribute = function(parent,attributeName,attributeValue)
for _,obj in pairs(parent:GetChildren()) do
if obj:FindFirstChild("Humanoid") and not players:GetPlayerFromCharacter(obj) then
if obj:GetAttribute(attributeName) == attributeValue then
return obj
end
end
end
end
-- set an NPC with the attribute name "Ted"
local npc = getNpcByAttribute(workspace,"name","Ted")
can you try putting it into the script?
local thing1 = game.Workspace["Franklin Walking From Part To Part"].Torso
local thing2 = game.Players.LocalPlayer.Character.Torso
local dist = (thing1.Position - thing2.Position).Magnitude
if dist <= 10000 then
wait(5)
print("player is less than 10 studs away from the NPC")
end
print(thing1.Name .. " is " .. dist .. " studs away from " .. thing2.Name)
can you make it so It finds the NPC and Player Magnitude @Jumpathy or @Diduiem ?
Let’s refine your script to ensure it works correctly. The main points to address are:
- Finding the closest player efficiently.
- Ensuring the NPC responds correctly based on the player’s message.
- Correcting any logical errors and improving the structure.
Here is an updated version of your script:
local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local modelRoot = script.Parent.HumanoidRootPart
local NPC = workspace:FindFirstChild("Franklin Walking From Part To Part")
-- Responses table with possible NPC responses
local responses = {
["Hello, What would you like to order?"] = {
"Can I get a Pizza",
"Can I get a Part",
""
},
}
-- Function to find the closest player to the NPC
local function getClosestPlayer()
local closestPlayer = nil
local closestDistance = 200 -- Maximum search distance
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local playerRoot = player.Character.HumanoidRootPart
local distance = (NPC.PrimaryPart.Position - playerRoot.Position).Magnitude
if distance < closestDistance then
closestPlayer = player
closestDistance = distance
end
end
end
return closestPlayer, closestDistance
end
-- Handle player chatting
local function onPlayerChatted(player, message)
local possibleResponses = responses[message]
if possibleResponses then
local playerRoot = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if playerRoot then
local distance = (playerRoot.Position - modelRoot.Position).Magnitude
if distance <= 10 then
task.wait(1)
local randomResponse = possibleResponses[math.random(1, #possibleResponses)]
if randomResponse ~= "" then
Chat:Chat(script.Parent.Head, randomResponse, Enum.ChatColor.Blue)
end
else
Chat:Chat(script.Parent.Head, "Get out of my sight!", Enum.ChatColor.Red)
end
end
end
end
-- Connect player added event
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
player.Chatted:Connect(function(message)
onPlayerChatted(player, message)
end)
end)
end)
-- For already existing players when the script runs
for _, player in pairs(Players:GetPlayers()) do
player.CharacterAdded:Connect(function(character)
player.Chatted:Connect(function(message)
onPlayerChatted(player, message)
end)
end)
end
Explanation:
-
getClosestPlayer:
- This function iterates over all players and calculates the distance to the NPC.
- It returns the closest player and the distance.
-
onPlayerChatted:
- This function handles when a player chats.
- It checks if the message is in the responses table and responds appropriately.
- It also checks the distance between the player and the NPC to determine if the NPC should respond or not.
-
PlayerAdded and CharacterAdded Connections:
- These connections ensure that the script listens for new players joining the game and their characters being added.
- It also handles already existing players when the script runs.
Okay so heres the problems:
Cannot get the tool script:
local R = game:GetService("ReplicatedStorage")
local npc = game.Workspace.Noob
local plr = game.Players.LocalPlayer
R.NpcEatEvent.OnServerEvent:Connect(function(player, tool, NPC)
wait(0.2)
local Tool = plr.Parent:GetPlayerFromCharacter(player.Character:WaitForChild("Pizza"))
if player.Character:FindFirstChild("Pizza") and npc and npc:FindFirstChild("Humanoid") then do
wait(2)
npc.Humanoid:EquipTool()
player.Character.Pizza:Activate(Tool)
end
end
end)
Second: It only finds the first npc even the closer ones: Script:
local Players = game:GetService("Players")
local ChatService = game:GetService("Chat")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local tool = script.Parent
local npc = game.Workspace.Noob
local prompt = npc.Head:FindFirstChild("ProximityPrompt")
local walkToPart = game.Workspace:FindFirstChild("WAlKtoWAITERpart")
local destroyPart = game.Workspace:FindFirstChild("Detroy")
local Event = ReplicatedStorage:FindFirstChild("NpcEatEvent")
if not (npc and prompt and walkToPart and destroyPart) then
warn("Ensure all required parts (NPC, ProximityPrompt, Walking part, Destroy part) are correctly named and available in the workspace.")
return
end
prompt.Enabled = false
local responses = {
"I didn't order this",
"Order Received",
}
local orderReceived = responses[2]
local notOrdered = responses[1]
local function chatWithNpc(npc, text)
ChatService:Chat(npc.Head, text, Enum.ChatColor.White)
end
prompt.Triggered:Connect(function(triggeringPlayer)
if triggeringPlayer == player and tool.Parent == player.Character then
if orderReceived == notOrdered then
chatWithNpc(npc, notOrdered)
npc.Humanoid:MoveTo(walkToPart.Position)
npc.Humanoid.MoveToFinished:Wait()
wait(1)
npc.Humanoid:MoveTo(destroyPart.Position)
npc.Humanoid.MoveToFinished:Wait()
wait(1)
npc:Destroy()
else
ReplicatedStorage.NpcEatEvent:FireServer(player, tool, NPC)
end
end
end)
tool.Equipped:Connect(function()
if tool then
prompt.Enabled = true
else
prompt.Enabled = false
end
chatWithNpc(npc, "")
end)
tool.Activated:Connect(function()
chatWithNpc(npc, "")
end)
Third: it will not equip the tool anymore so can anyone help?
@RobloxHasTalentR or @Diduiem or somebody.