0% found this document useful (0 votes)
42 views4 pages

Message

This document contains scripts for an NPC dialogue system in a game. It includes: 1) An NPC info module that defines dialogue text, responses, and camera settings. 2) A server script that connects players to NPCs using a proximity trigger. 3) A GUI script that displays dialogue text and response options, and handles progressing between dialogue lines.

Uploaded by

João Lucas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Message

This document contains scripts for an NPC dialogue system in a game. It includes: 1) An NPC info module that defines dialogue text, responses, and camera settings. 2) A server script that connects players to NPCs using a proximity trigger. 3) A GUI script that displays dialogue text and response options, and handles progressing between dialogue lines.

Uploaded by

João Lucas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

----MODULE SCRIPT - IN THE NPC ----

local Info = {}

Info.LeaveMessage = "Goodbye, I'll see you around"


Info.CameraDistance = 3

Info.Dialog = {
[1] = {
Text = "Hello, how are you?";
Choices = {
[1] = { Text = "Bad"; };
[2] = { Text = "Good, how are you?"; Next = 3 };
[3] = { Text = "Im alright"; Next = 4 };
}
};
[2] = { Text = "Im sorry to hear that"; };
[3] = {
Text = "Im great, thanks for asking";
Choices = {
[1] = { Text = "What's your name?"; Next = 5; Follow = true };
[2] = { Text = "Where are you from?"; Next = 6; Follow = true };
[4] = { Text = "Nice weather, right?"; Next = 7; Follow = true };
}
};
[4] = {
Text = "I feel the same way"; };

[5] = {
Text = "My name is NPC 1";
Choices = {}
};

[6] = {
Text = "I'm from the Baseplate";
Choices = {}
};

[7] = {
Text = "Mmmmmmh definitely";
Choices = {}
};
}

return Info

----SERVER SCRIPT - IN NPCs FOLDER ----


local replicated = game:GetService("ReplicatedStorage")

local event = replicated:WaitForChild("TalkEvent")

for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Model") and v:FindFirstChild("Humanoid") then
local hrp = v:WaitForChild("HumanoidRootPart")
local head = v:WaitForChild("Head")

local module = require(v.Info)

local prompt = Instance.new("ProximityPrompt", hrp)


prompt.ActionText = "Talk"
prompt.ObjectText = v.Name
prompt.RequiresLineOfSight = false

prompt.Triggered:Connect(function(player)
if module.CameraDistance then
event:FireClient(player, v, module, CFrame.new(head.Position +
head.CFrame.LookVector * module.CameraDistance, head.Position))
else
event:FireClient(player, v, module)
end
end)
end
end

----LOCAL SCRIPT - INSIDE THE SCREEN GUI ----

local tweenService = game:GetService("TweenService")


local replicated = game:GetService("ReplicatedStorage")

local event = replicated:WaitForChild("TalkEvent")

local camera = workspace.CurrentCamera

local main = script.Parent.Main


local choicesFrame = main.ChoicesFrame
local nameLabel = main.NameFrame.Label
local dialogLabel = main.DialogFrame.Label
local choiceTemplate = script.ChoiceTemplate

local currentNpc = nil


local storedChoices = {}

local function typeWrite(text, label)


label.Text = ""

for i, v in text:split("") do
label.Text = label.Text .. v
task.wait(0.05)
end
end

local function endDialog(info)


for i, child in pairs(choicesFrame:GetChildren()) do
if child:IsA("TextButton") then
child:Destroy()
end
end

typeWrite(info.LeaveMessage, dialogLabel)

task.wait(0.5)

currentNpc.HumanoidRootPart.ProximityPrompt.Enabled = true
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = game.Players.LocalPlayer.Character

main:TweenPosition(UDim2.new(0,0,1,0))
task.wait(1)
main.Visible = false
nameLabel.Text = ""
dialogLabel.Text = ""
storedChoices = {}

currentNpc = nil
end

local function nextDialog(info, lastChoice)


for i, child in pairs(choicesFrame:GetChildren()) do
if child:IsA("TextButton") then
child:Destroy()
end
end

local dialog
if lastChoice then
if lastChoice.Next then
dialog = info.Dialog[lastChoice.Next]
else
endDialog(info)
end
else
dialog = info.Dialog[1]
end

if not dialog then return end

typeWrite(dialog.Text, dialogLabel)

if dialog.Choices and (#dialog.Choices > 0 or #storedChoices > 0) then


for i, choice in pairs(storedChoices) do
local clone = choiceTemplate:Clone()
clone.Parent = choicesFrame
clone.Name = choice.Text
clone.Label.Text = choice.Text

clone.MouseButton1Click:Connect(function()
storedChoices[i] = nil
nextDialog(info, choice)
end)
end

for i, choice in dialog.Choices do


local clone = choiceTemplate:Clone()
clone.Parent = choicesFrame
clone.Name = choice.Text
clone.Label.Text = choice.Text

if choice.Follow == true then


storedChoices[i] = choice
end

clone.MouseButton1Click:Connect(function()
storedChoices[i] = nil
nextDialog(info, choice)
end)
end

local leave = choiceTemplate:Clone()


leave.Parent = choicesFrame
leave.Name = "Leave"
leave.BackgroundColor3 = Color3.fromRGB(77, 35, 36)
leave.Label.Text = "I have to go"

leave.MouseButton1Click:Connect(function()
endDialog(info)
end)
else
task.wait(1)

if dialog.Next then
nextDialog(info, dialog)
else
endDialog(info)
end
end
end

event.OnClientEvent:Connect(function(npc, info, cameraCF)


if currentNpc == nil and npc then
currentNpc = npc

nameLabel.Text = npc.Name
dialogLabel.Text = ""
storedChoices = {}

for i, child in pairs(choicesFrame:GetChildren()) do


if child:IsA("TextButton") then
child:Destroy()
end
end

if cameraCF then
npc.HumanoidRootPart.ProximityPrompt.Enabled = false

camera.CameraType = Enum.CameraType.Scriptable
tweenService:Create(workspace.CurrentCamera, TweenInfo.new(2,
Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {CFrame = cameraCF}):Play()
end

main.Position = UDim2.new(0,0,1,0)
main.Visible = true

main:TweenPosition(UDim2.new(0,0,0,0))

task.wait(1)
nextDialog(info, nil)
end
end)

You might also like