Guest User

Untitled

a guest
Jan 31st, 2026
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local UserInputService = game:GetService("UserInputService")
  3. local TweenService = game:GetService("TweenService")
  4. local Players = game:GetService("Players")
  5.  
  6. local DialogueData = require(ReplicatedStorage:WaitForChild("DialogueData"))
  7. local StartDialogueEvent = ReplicatedStorage:WaitForChild("StartDialogueEvent")
  8.  
  9. local player = Players.LocalPlayer
  10. local gui = script.Parent
  11. local mainFrame = gui:WaitForChild("MainFrame")
  12. local portraitImage = mainFrame:WaitForChild("Portrait")
  13. local nameLabel = mainFrame:WaitForChild("NameLabel")
  14. local dialogueLabel = mainFrame:WaitForChild("DialogueLabel")
  15. local buttonContainer = mainFrame:WaitForChild("ButtonContainer")
  16. local typeSound = mainFrame:WaitForChild("TypeSound")
  17.  
  18. local optionButtons = {
  19. buttonContainer:WaitForChild("Option1"),
  20. buttonContainer:WaitForChild("Option2"),
  21. buttonContainer:WaitForChild("Option3"),
  22. }
  23.  
  24.  
  25. local TYPE_SPEED = 0.03
  26. local TWEEN_INFO = TweenInfo.new(0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
  27.  
  28. local isTyping = false
  29. local skipRequested = false
  30. local dialogueOpen = false
  31. local currentNpcData = nil
  32. local buttonConnections = {}
  33. local currentPrompt = nil
  34.  
  35. mainFrame.Visible = false
  36. mainFrame.Position = UDim2.new(0.5, 0, 1.1, 0)
  37.  
  38. local function playTypeSound()
  39. typeSound.PlaybackSpeed = 0.9 + (math.random() * 0.2)
  40. typeSound:Play()
  41. end
  42.  
  43. local function togglePlayerMovement(canMove)
  44. local character = player.Character
  45. if character then
  46. local humanoid = character:FindFirstChildOfClass("Humanoid")
  47. if humanoid then
  48. if canMove then
  49. humanoid.WalkSpeed = canMove and 16 or 0
  50. humanoid.JumpPower = canMove and 50 or 0
  51. else
  52. humanoid.WalkSpeed = 0
  53. humanoid.JumpPower = 0
  54. end
  55. end
  56. end
  57. end
  58.  
  59. local function typewriteText(textToType)
  60. isTyping = true
  61. skipRequested = false
  62. dialogueLabel.Text = ""
  63.  
  64. for i = 1, #textToType do
  65. if skipRequested then
  66. dialogueLabel.Text = textToType
  67. playTypeSound()
  68. break
  69. end
  70.  
  71. dialogueLabel.Text = string.sub(textToType, 1, i)
  72.  
  73. local lastChar = string.sub(textToType, i, i)
  74. if lastChar ~= " " then
  75. playTypeSound()
  76. end
  77.  
  78. task.wait(TYPE_SPEED)
  79. end
  80.  
  81. isTyping = false
  82. skipRequested = false
  83. end
  84.  
  85. local function closeDialogue()
  86. if not dialogueOpen then return end
  87. dialogueOpen = false
  88.  
  89. if currentPrompt then
  90. currentPrompt.Enabled = true
  91. currentPrompt = nil
  92. end
  93.  
  94. togglePlayerMovement(true)
  95.  
  96. local targetPos = UDim2.new(0.5, 0, 1.1, 0)
  97. local tween = TweenService:Create(mainFrame, TWEEN_INFO, {Position = targetPos})
  98. tween:Play()
  99. tween:Completed:Wait()
  100. mainFrame.Visible = false
  101. currentNpcData = nil
  102. end
  103.  
  104. local function displayNode(nodeId)
  105. if not currentNpcData then return end
  106.  
  107. local nodeData = currentNpcData.Nodes[nodeId]
  108.  
  109. if not nodeData then
  110. closeDialogue()
  111. return
  112. end
  113.  
  114. for i, button in ipairs(optionButtons) do
  115. local choiceData = nodeData.Choices and nodeData.Choices[i]
  116.  
  117. if buttonConnections[i] then
  118. buttonConnections[i]:Disconnect()
  119. buttonConnections[i] = nil
  120. end
  121.  
  122. if choiceData then
  123. button.Text = choiceData.Text
  124. button.Visible = true
  125.  
  126. local connection = button.MouseButton1Click:Connect(function()
  127. if isTyping then return end
  128.  
  129. if choiceData.Next == "EXIT" then
  130. closeDialogue()
  131. else
  132. displayNode(choiceData.Next)
  133. end
  134. end)
  135.  
  136. buttonConnections[i] = connection
  137. else
  138. button.Visible = false
  139. end
  140. end
  141.  
  142. typewriteText(nodeData.Text)
  143. end
  144.  
  145. local function openDialogue(npcName)
  146. local data = DialogueData[npcName]
  147. if not data then
  148. return
  149. end
  150.  
  151. togglePlayerMovement(false)
  152.  
  153. local npcModel = workspace:FindFirstChild(npcName)
  154. if npcModel then
  155. local prompt = npcModel:FindFirstChildOfClass("ProximityPrompt", true)
  156. if prompt then
  157. currentPrompt = prompt
  158. prompt.Enabled = false
  159. end
  160. end
  161.  
  162. currentNpcData = data
  163. dialogueOpen = true
  164. mainFrame.Visible = true
  165.  
  166. nameLabel.Text = npcName
  167. portraitImage.Image = data.PortraitId
  168.  
  169. local targetPos = UDim2.new(0.5, 0, 0.95, 0)
  170. mainFrame.Position = UDim2.new(0.5, 0, 1.1, 0)
  171. TweenService:Create(mainFrame, TWEEN_INFO, {Position = targetPos}):Play()
  172.  
  173. displayNode(data.StartNode)
  174. end
  175.  
  176. StartDialogueEvent.OnClientEvent:Connect(openDialogue)
  177.  
  178. UserInputService.InputBegan:Connect(function(input, gameProcessed)
  179. if isTyping and (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
  180. skipRequested = true
  181. end
  182. end)
Advertisement
Add Comment
Please, Sign In to add comment