When engaging with an NPC within the Roblox editor, the dialog consistently opens every time. However, upon publishing to Roblox and playing the game, the dialog functions only during the initial interaction. If the player exits and rejoins, the dialog fails to reopen.
In my initial attempt to address this issue, I modified the following line of code:
From:
local player = game.Players.LocalPlayer
To:
local player = Players.LocalPlayer
This adjustment enabled the dialog to function for the first encounter but did not resolve the issue of it failing to reopen upon subsequent player exits and rejoins.
Rest of Code
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 NPCName = DialogFrame.NPCName
local player = Players.LocalPlayer
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
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
local function onDialog(dialog, index, proximityPrompt)
if dialog[index] then
gradualText(dialog[index])
else
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
end
end
InputButton.MouseButton1Click:Connect(function()
if gradualTextInProgress then
return
end
local dialog = DialogModule[NPCName.Value]
dialogIndex = dialogIndex + 1
onDialog(dialog, dialogIndex, NPCs[NPCName.Value].HumanoidRootPart.ProximityPrompt)
end)
for _, NPC in pairs(NPCs:GetChildren()) do
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]
proximityPrompt.Triggered:Connect(function()
if dialogOpen then
return
end
dialogOpen = true
proximityPrompt.Enabled = false
NPCName.Value = NPC.Name
p.Character.HumanoidRootPart.Anchored = true
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