Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CODE BREAKDOWN & HOW TO SET-UP: https://youtu.be/BZrsNgzfMm4
- --Settings:
- local InteractKeyCode = Enum.KeyCode.E
- local CameraTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
- -- Services:
- local RS = game:GetService("ReplicatedStorage")
- local RunService = game:GetService("RunService")
- local TweenService = game:GetService("TweenService")
- local UIS = game:GetService("UserInputService")
- -- Player/Char Stuff:
- local Player = game.Players.LocalPlayer
- repeat wait() until Player.Character
- local Character = Player.Character
- local Humanoid = Character:WaitForChild("Humanoid")
- local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
- -- Objects:
- local NpcFolder = workspace:WaitForChild("Npc's")
- local CurrentCamera = workspace.CurrentCamera
- CurrentCamera.CameraType = Enum.CameraType.Custom
- CurrentCamera.CameraSubject = Humanoid
- -- Elements:
- local DialougeUI = script.Parent
- local HolderUI = DialougeUI:WaitForChild("Holder")
- local ChoicesUI = HolderUI:WaitForChild("Choices")
- local Text = HolderUI:WaitForChild("Text")
- --\\-- FX & Extra UI --//--
- local ObjectsFolder = script:WaitForChild("Objects")
- local DialogFX = ObjectsFolder:WaitForChild("DialogFX")
- local ParticleEmitter = DialogFX:WaitForChild("ParticleEmitter")
- local BillboardGui = DialogFX:WaitForChild("BillboardGui")
- local BasicTextFont = ObjectsFolder:WaitForChild("BasicTextFont")
- -- Variables
- local CurrentDialog = nil
- local InDialog = false
- -- Functions:
- function SearchForDialouges()
- local DialogTable = {}
- for index, value in ipairs(NpcFolder:GetDescendants()) do
- if value:IsA("Dialog") and value.Parent:IsA("BoolValue") and value.Parent.Value == true then
- table.insert(DialogTable, table.getn(DialogTable) + 1, value)
- end
- end
- return DialogTable
- end
- function FindClosestDialog(DialogTable)
- local Closest
- local PlayerPosition = Character.PrimaryPart.Position
- for i, v in pairs(DialogTable) do
- if Closest == nil then
- Closest = v
- else
- if (PlayerPosition - v.Parent.Parent.Position).magnitude < (Closest.Parent.Parent.Position - PlayerPosition).magnitude then
- Closest = v
- end
- end
- end
- return Closest
- end
- function CreateChoices(Dialog)
- local SelectedChoice = false
- local Choices = {}
- for i, v in ipairs(Dialog:GetChildren()) do
- if v:IsA("DialogChoice") then
- table.insert(Choices, table.getn(Choices) + 1, v)
- end
- end
- for i, v in pairs(Choices) do
- local OffSet = 1/table.getn(Choices)
- local Clone = BasicTextFont:Clone()
- Clone.Parent = ChoicesUI
- Clone.Text = tostring(v.UserDialog)
- Clone.Size = UDim2.new(OffSet,0,1,0)
- Clone.Position = UDim2.new(OffSet*(i - 1),0,0,0)
- Clone.MouseButton1Click:Connect(function()
- SelectedChoice = v
- end)
- end
- repeat wait() until SelectedChoice ~= false or table.getn(Choices) <= 0
- return SelectedChoice
- end
- function StartDialog()
- if CurrentDialog ~= nil and CurrentCamera.CameraType ~= Enum.CameraType.Scriptable and InDialog == false then
- local HostPart = CurrentDialog.Parent.Parent
- InDialog = true
- CurrentCamera.CameraType = Enum.CameraType.Scriptable
- local CamCF = CFrame.lookAt(CurrentCamera.CFrame.Position, HostPart.Position)
- local CameraTween = TweenService:Create(CurrentCamera, CameraTweenInfo, {CFrame = CamCF})
- local TextTransparencyTweenDark = TweenService:Create(Text, TweenInfo.new(1), {BackgroundTransparency = 0.5})
- TextTransparencyTweenDark:Play()
- CameraTween:Play()
- local DialogSelected = CurrentDialog
- Text.Text = DialogSelected.InitialPrompt
- if DialogSelected:FindFirstChildWhichIsA("DialogChoice") ~= nil then
- local Choice = CreateChoices(DialogSelected)
- if Choice:FindFirstChildWhichIsA("DialogChoice") ~= nil then
- repeat wait() until Choice
- ChoicesUI:ClearAllChildren()
- DialogSelected = Choice
- Text.Text = DialogSelected.ResponseDialog
- repeat
- local Choice = CreateChoices(DialogSelected)
- repeat wait() until Choice
- if Choice ~= false then
- ChoicesUI:ClearAllChildren()
- DialogSelected = Choice
- Text.Text = DialogSelected.ResponseDialog
- end
- until DialogSelected:FindFirstChildWhichIsA("DialogChoice") == nil or Choice == false
- else -- No other choices.
- Text.Text = Choice.ResponseDialog
- end
- end
- ChoicesUI:ClearAllChildren()
- wait(1)
- Text.Text = ""
- Text.BackgroundTransparency = 1
- CurrentCamera.CameraType = Enum.CameraType.Custom
- CurrentCamera.CameraSubject = Humanoid
- InDialog = false
- end
- end
- -- Running:
- local Dialouges = SearchForDialouges()
- RunService.Stepped:Connect(function()
- local ClosestDialog = FindClosestDialog(Dialouges)
- local HostPart = ClosestDialog.Parent.Parent
- local Magnitude = (HumanoidRootPart.Position - ClosestDialog.Parent.Parent.Position).Magnitude
- if Magnitude <= ClosestDialog.ConversationDistance then
- DialogFX.Parent = workspace
- DialogFX.Position = HostPart.Position
- BillboardGui.Enabled = true
- ParticleEmitter.Enabled = true
- CurrentDialog = ClosestDialog
- else
- ParticleEmitter.Enabled = false
- BillboardGui.Enabled = false
- DialogFX.Parent = ObjectsFolder
- CurrentDialog = nil
- end
- end)
- UIS.InputBegan:Connect(function(input)
- if input.UserInputType == Enum.UserInputType.Keyboard then
- if input.KeyCode == InteractKeyCode then
- StartDialog()
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement