local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local DialogModule = require(ReplicatedStorage.DialogModule)
local p = Players.LocalPlayer
local NPCs = workspace.NPCs
local DialogFrame = script.Parent.DialogFrame
local InputButton = DialogFrame.Input
local Option1Button = DialogFrame.Option1Button -- Primeiro botão de opção
local Option2Button = DialogFrame.Option2Button -- Segundo botão de opção
local NPCName = DialogFrame.NPCName
local image = DialogFrame.fot
local DIALOG_TWEENINFO = TweenInfo.new(0.25, Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out)
local dialogOpen = false
local dialogTween = nil
local dialogIndex = 0
local gradualTextInProgress = false
-- Função para exibir o texto gradualmente
local function gradualText(text)
if gradualTextInProgress then
return
end
local length = string.len(text)
for i = 1, length, 1 do
gradualTextInProgress = true
DialogFrame.DialogText.Text = string.sub(text, 1, i)
wait()
end
gradualTextInProgress = false
end
-- Função para lidar com o diálogo
local function onDialog(dialog, index, proximityPrompt)
if dialog[index] then
gradualText(dialog[index])
-- Ativa os botões de resposta quando o diálogo estiver rodando
Option1Button.Visible = true
Option2Button.Visible = true
-- Define o texto dos botões a partir do módulo
local responses = DialogModule[NPCName.Value].Responses
Option1Button.Text = responses.Option1.Text
Option2Button.Text = responses.Option2.Text
else
-- Se o diálogo terminar, fecha o painel e libera o jogador
if dialogTween then
dialogTween:Cancel()
dialogTween = nil
end
local tween = TweenService:Create(DialogFrame, DIALOG_TWEENINFO, {
Position = UDim2.new(0, 0, 2, 0)
})
dialogTween = tween
dialogTween:Play()
proximityPrompt.Enabled = true
dialogOpen = false
dialogIndex = 0
p.Character.HumanoidRootPart.Anchored = false
-- Esconde os botões quando o diálogo acaba
Option1Button.Visible = false
Option2Button.Visible = false
end
end
-- Função para terminar o diálogo com a despedida personalizada
local function endDialogWithGoodbye()
local goodbyeMessage = DialogModule[NPCName.Value].Goodbye
DialogFrame.DialogText.Text = goodbyeMessage
wait(2) -- Espera 2 segundos antes de fechar o diálogo
-- Fecha o diálogo
if dialogTween then
dialogTween:Cancel()
dialogTween = nil
end
local tween = TweenService:Create(DialogFrame, DIALOG_TWEENINFO, {
Position = UDim2.new(0, 0, 2, 0)
})
dialogTween = tween
dialogTween:Play()
-- Libera o jogador
NPCs[NPCName.Value].HumanoidRootPart.ProximityPrompt.Enabled = true
dialogOpen = false
dialogIndex = 0
p.Character.HumanoidRootPart.Anchored = false
-- Esconde os botões ao encerrar o diálogo
Option1Button.Visible = false
Option2Button.Visible = false
end
-- Lida com o primeiro botão de resposta
Option1Button.MouseButton1Click:Connect(function()
local response = DialogModule[NPCName.Value].Responses.Option1.Response
DialogFrame.DialogText.Text = response
wait(2) -- Mostra a resposta por 2 segundos
endDialogWithGoodbye()
end)
-- Lida com o segundo botão de resposta
Option2Button.MouseButton1Click:Connect(function()
local response = DialogModule[NPCName.Value].Responses.Option2.Response
DialogFrame.DialogText.Text = response
wait(2) -- Mostra a resposta por 2 segundos
endDialogWithGoodbye()
end)
-- Lida com o início do diálogo
for _, NPC in pairs(NPCs:GetChildren()) do
wait(0.1)
local humanoidRootPart = NPC:FindFirstChild("HumanoidRootPart")
local proximityPrompt = humanoidRootPart:FindFirstChild("ProximityPrompt")
if humanoidRootPart and proximityPrompt then
proximityPrompt.ObjectText = NPC.Name
proximityPrompt.ActionText = "Chat with " .. NPC.Name
local dialog = DialogModule[NPC.Name].Dialog
proximityPrompt.Triggered:Connect(function()
if dialogOpen then
return
end
dialogOpen = true
proximityPrompt.Enabled = false
NPCName.Value = NPC.Name
p.Character.HumanoidRootPart.Anchored = true
image.Image = NPCs[NPCName.Value].fot.Image
if dialogTween then
dialogTween:Cancel()
dialogTween = nil
end
local tween = TweenService:Create(DialogFrame, DIALOG_TWEENINFO,
{
Position = UDim2.new(0, 0, 1, 0)
})
dialogTween = tween
dialogTween:Play()
dialogIndex = 1
onDialog(dialog, dialogIndex, proximityPrompt)
end)
end
end