Advertisement
123456789_players

Sal's Roblox Dialogue System

Feb 3rd, 2022
2,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. -- CODE BREAKDOWN & HOW TO SET-UP: https://youtu.be/BZrsNgzfMm4
  2.  
  3. --Settings:
  4. local InteractKeyCode = Enum.KeyCode.E
  5. local CameraTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
  6.  
  7. -- Services:
  8.  
  9. local RS = game:GetService("ReplicatedStorage")
  10. local RunService = game:GetService("RunService")
  11. local TweenService = game:GetService("TweenService")
  12. local UIS = game:GetService("UserInputService")
  13. -- Player/Char Stuff:
  14.  
  15. local Player = game.Players.LocalPlayer
  16. repeat wait() until Player.Character
  17. local Character = Player.Character
  18. local Humanoid = Character:WaitForChild("Humanoid")
  19. local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
  20.  
  21. -- Objects:
  22.  
  23. local NpcFolder = workspace:WaitForChild("Npc's")
  24. local CurrentCamera = workspace.CurrentCamera
  25. CurrentCamera.CameraType = Enum.CameraType.Custom
  26. CurrentCamera.CameraSubject = Humanoid
  27.  
  28. -- Elements:
  29.  
  30. local DialougeUI = script.Parent
  31. local HolderUI = DialougeUI:WaitForChild("Holder")
  32. local ChoicesUI = HolderUI:WaitForChild("Choices")
  33. local Text = HolderUI:WaitForChild("Text")
  34.  
  35. --\\-- FX & Extra UI --//--
  36.  
  37. local ObjectsFolder = script:WaitForChild("Objects")
  38. local DialogFX = ObjectsFolder:WaitForChild("DialogFX")
  39. local ParticleEmitter = DialogFX:WaitForChild("ParticleEmitter")
  40. local BillboardGui = DialogFX:WaitForChild("BillboardGui")
  41. local BasicTextFont = ObjectsFolder:WaitForChild("BasicTextFont")
  42.  
  43. -- Variables
  44. local CurrentDialog = nil
  45. local InDialog = false
  46.  
  47. -- Functions:
  48. function SearchForDialouges()
  49.     local DialogTable = {}
  50.  
  51.     for index, value in ipairs(NpcFolder:GetDescendants()) do
  52.         if value:IsA("Dialog") and value.Parent:IsA("BoolValue") and value.Parent.Value == true then
  53.             table.insert(DialogTable, table.getn(DialogTable) + 1, value)
  54.         end
  55.     end
  56.  
  57.     return DialogTable
  58. end
  59.  
  60. function FindClosestDialog(DialogTable)
  61.     local Closest
  62.     local PlayerPosition = Character.PrimaryPart.Position
  63.  
  64.     for i, v in pairs(DialogTable) do
  65.  
  66.         if Closest == nil then
  67.             Closest = v
  68.         else
  69.             if (PlayerPosition - v.Parent.Parent.Position).magnitude < (Closest.Parent.Parent.Position - PlayerPosition).magnitude then
  70.                 Closest = v
  71.             end
  72.         end
  73.  
  74.     end
  75.     return Closest
  76. end
  77.  
  78. function CreateChoices(Dialog)
  79.     local SelectedChoice = false
  80.     local Choices = {}
  81.  
  82.     for i, v in ipairs(Dialog:GetChildren()) do
  83.         if v:IsA("DialogChoice") then
  84.             table.insert(Choices, table.getn(Choices) + 1, v)
  85.         end
  86.     end
  87.  
  88.     for i, v in pairs(Choices) do
  89.  
  90.         local OffSet = 1/table.getn(Choices)
  91.         local Clone = BasicTextFont:Clone()
  92.         Clone.Parent = ChoicesUI
  93.         Clone.Text = tostring(v.UserDialog)
  94.         Clone.Size = UDim2.new(OffSet,0,1,0)
  95.         Clone.Position = UDim2.new(OffSet*(i - 1),0,0,0)
  96.  
  97.         Clone.MouseButton1Click:Connect(function()
  98.             SelectedChoice = v
  99.         end)
  100.  
  101.     end
  102.     repeat wait() until SelectedChoice ~= false or table.getn(Choices) <= 0
  103.     return SelectedChoice
  104. end
  105.  
  106. function StartDialog()
  107.     if CurrentDialog ~= nil and CurrentCamera.CameraType ~= Enum.CameraType.Scriptable and InDialog == false then
  108.         local HostPart = CurrentDialog.Parent.Parent
  109.         InDialog = true
  110.         CurrentCamera.CameraType = Enum.CameraType.Scriptable
  111.         local CamCF = CFrame.lookAt(CurrentCamera.CFrame.Position, HostPart.Position)
  112.         local CameraTween = TweenService:Create(CurrentCamera, CameraTweenInfo, {CFrame = CamCF})
  113.         local TextTransparencyTweenDark = TweenService:Create(Text, TweenInfo.new(1), {BackgroundTransparency = 0.5})
  114.         TextTransparencyTweenDark:Play()
  115.         CameraTween:Play()
  116.  
  117.         local DialogSelected = CurrentDialog
  118.         Text.Text = DialogSelected.InitialPrompt
  119.  
  120.         if DialogSelected:FindFirstChildWhichIsA("DialogChoice") ~= nil then
  121.             local Choice = CreateChoices(DialogSelected)
  122.             if Choice:FindFirstChildWhichIsA("DialogChoice") ~= nil then
  123.                 repeat wait() until Choice
  124.                 ChoicesUI:ClearAllChildren()
  125.                 DialogSelected = Choice
  126.                 Text.Text = DialogSelected.ResponseDialog
  127.  
  128.                 repeat
  129.  
  130.                     local Choice = CreateChoices(DialogSelected)
  131.                     repeat wait() until Choice
  132.                     if Choice ~= false then
  133.                         ChoicesUI:ClearAllChildren()
  134.                         DialogSelected = Choice
  135.                         Text.Text = DialogSelected.ResponseDialog
  136.                     end
  137.                 until DialogSelected:FindFirstChildWhichIsA("DialogChoice") == nil or Choice == false
  138.                
  139.             else -- No other choices.
  140.                 Text.Text = Choice.ResponseDialog
  141.             end
  142.         end
  143.         ChoicesUI:ClearAllChildren()
  144.         wait(1)
  145.         Text.Text = ""
  146.         Text.BackgroundTransparency = 1
  147.         CurrentCamera.CameraType = Enum.CameraType.Custom
  148.         CurrentCamera.CameraSubject = Humanoid
  149.         InDialog = false
  150.     end
  151. end
  152.  
  153.  
  154. -- Running:
  155. local Dialouges = SearchForDialouges()
  156.  
  157. RunService.Stepped:Connect(function()
  158.     local ClosestDialog = FindClosestDialog(Dialouges)
  159.     local HostPart = ClosestDialog.Parent.Parent
  160.     local Magnitude = (HumanoidRootPart.Position - ClosestDialog.Parent.Parent.Position).Magnitude
  161.  
  162.     if Magnitude <= ClosestDialog.ConversationDistance then
  163.         DialogFX.Parent = workspace
  164.         DialogFX.Position = HostPart.Position
  165.         BillboardGui.Enabled = true
  166.         ParticleEmitter.Enabled = true
  167.         CurrentDialog = ClosestDialog
  168.     else
  169.         ParticleEmitter.Enabled = false
  170.         BillboardGui.Enabled = false
  171.         DialogFX.Parent = ObjectsFolder
  172.         CurrentDialog = nil
  173.     end
  174.  
  175. end)
  176.  
  177. UIS.InputBegan:Connect(function(input)
  178.     if input.UserInputType == Enum.UserInputType.Keyboard then
  179.         if input.KeyCode == InteractKeyCode then
  180.             StartDialog()
  181.         end
  182.     end
  183. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement