I have an ingame mechanic where you can speak to an npc, like every other game the npc gives talks to you, player replys through the preselected answer choices the game gives them, then the npc responds based on what the player chooses.
To clean up my code I have a module in replicated storage that writes out the text given through a parameter, using a for i loop which typewrites the text. I also have a tween function that tweens the frame whenever the player is gone talking with the npc. Module below
local DialougeModule = {
}
function DialougeModule.TypeWrite(Label, text, length)
for i = 1, #text, 1 do
Label.Text = string.sub(text, 1, i) -- Types out the text
--sound:Play() -- Plays the sound every letter
task.wait(length) -- How long it takes for each letter to pop up
end
end
function DialougeModule.TweenUi(Frame, Transparency, visibility)
local TweenService = game:GetService("TweenService")
Frame.Visible = visibility
local TI = TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local Tween = TweenService:Create(Frame, TI, {BackgroundTransparency = Transparency})
Tween:Play()
end
return DialougeModule
How i plan to make the npcs is simply having a serverscript under the npc in workspace.
In that serverscript, this is my code:
--[[//Services\\]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--[[//Modules\\]] --
local DialougeModule = require(ReplicatedStorage:WaitForChild("DialougeModule"))
--[[//References\\]] --
local ProximityPrompt = script.Parent.NPCInteract
--[[//Debounce\\]] --
--[[//Dialouges\\]] --
Dialouge = {
DialougeContent = {
Content = "Need Something?";
ReplyOne = "Yeah, are you hiring? I am looking for a job and I saw the help wanted poster outside.";
ReplyTwo = "Nevermind.";
Content2 = {
ReplyOneContent = "Na, we aren't hiring. I forgot to take the poster down.";
ReplyOne = "Oh okay.";
ReplyTwo = "Alright, bye."
};
Content3 = {
ReplyOneContent = "They might be hiring down the street, one of them food stores, check them out.";
ReplyOne = "Thanks, I'll check them out.";
ReplyTwo = "Nevermind, not interested in that, bye."
};
}
}
--[[//Script\\]] --
ProximityPrompt.Triggered:Connect(function(player)
print("Triggered")
local PlayerUi = player.PlayerGui
local DialougeScreen = PlayerUi:WaitForChild('DialougeScreen')
local DialougeFrame = DialougeScreen:WaitForChild("DialougeFrame")
local Name = DialougeFrame.NPCName
local Choice1 = DialougeFrame.ChoiceButton1
local Choice2 = DialougeFrame.ChoiceButton2
local NpcText = DialougeFrame.NPCText
DialougeModule.TweenUi(DialougeFrame, 0, true)
task.wait(.5)
DialougeModule.TypeWrite(NpcText, Dialouge.DialougeContent.Content, 0.01)
Choice1.Visible = true
Choice2.Visible = true
Choice1.Text = Dialouge.DialougeContent.ReplyOne
Choice2.Text = Dialouge.DialougeContent.ReplyTwo
Choice2.MouseButton1Click:Connect(function()
DialougeModule.TweenUi(DialougeFrame, 1, false)
end)
Choice1.MouseButton1Click:Connect(function()
Choice1.Visible = false
Choice2.Visible = false
DialougeModule.TypeWrite(NpcText, Dialouge.DialougeContent.Content2.ReplyOneContent)
Choice1.Visible = true
Choice2.Visible = true
Choice1.Text = Dialouge.DialougeContent.Content2.ReplyOne
Choice2.Text = Dialouge.DialougeContent.Content2.ReplyTwo
Choice2.MouseButton1Click:Connect(function()
DialougeModule.TweenUi(DialougeFrame, 1, false)
end)
Choice1.MouseButton1Click:Connect(function()
Choice1.Visible = false
Choice2.Visible = false
DialougeModule.TypeWrite(NpcText, Dialouge.DialougeContent.Content3.ReplyOneContent)
Choice1.Visible = true
Choice2.Visible = true
Choice1.Text = Dialouge.DialougeContent.Content3.ReplyOne
Choice2.Text = Dialouge.DialougeContent.Content3.ReplyTwo
Choice1.MouseButton1Click:Connect(function()
DialougeModule.TweenUi(DialougeFrame, 1, false)
end)
Choice2.MouseButton1Click:Connect(function()
DialougeModule.TweenUi(DialougeFrame, 1, false)
end)
end)
end)
end)
There are two problems, whenever I try to talk to the npc, the npc typewrites just fine, but it typewrites the line before, however suddenly begins typewriting the next line.
The second problem is after you are done with the conversation, and try to talk with the npcs again, you cant go more than the first line. Video shown below to get a visual understanding