Hey I’m currently working on a single player experience and cant quite figure out how to go about this!
I am trying to create a dialogue system that works in first person. I only see stuff on how to do it for third person.
Essentially i want the character to go through a part to activate it and have a dialogue box pop up on screen. then i want the options of talking to the Npc to pop up attached to the npc floating after (as shown below)
local dialoguePart = workspace:WaitForChild("DialogueTriggerPart")
local npc = workspace:WaitForChild("NPC")
local player = game.Players.LocalPlayer
local playerTouched = false
-- Create the BillboardGui
local dialogueGui = Instance.new("BillboardGui")
dialogueGui.Size = UDim2.new(0, 400, 0, 200)
dialogueGui.StudsOffset = Vector3.new(0, 3, 0) -- Adjust offset
dialogueGui.AlwaysOnTop = true
dialogueGui.Enabled = false
-- Create TextLabel for Dialogue Text
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(1, 0, 0.5, 0)
textLabel.Position = UDim2.new(0, 0, 0, 0)
textLabel.TextScaled = true
textLabel.BackgroundTransparency = 0.5
textLabel.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
textLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
textLabel.Text = "Dialogue Text Here"
textLabel.Parent = dialogueGui
-- Create Frame for Choices
local choicesFrame = Instance.new("Frame")
choicesFrame.Size = UDim2.new(1, 0, 0.5, 0)
choicesFrame.Position = UDim2.new(0, 0, 0.5, 0)
choicesFrame.BackgroundTransparency = 1
choicesFrame.Parent = dialogueGui
-- Create Choice Buttons
local choiceButton1 = Instance.new("TextButton")
choiceButton1.Size = UDim2.new(1, 0, 0.5, 0)
choiceButton1.Position = UDim2.new(0, 0, 0, 0)
choiceButton1.Text = "Option 1"
choiceButton1.TextScaled = true
choiceButton1.BackgroundTransparency = 0.5
choiceButton1.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
choiceButton1.TextColor3 = Color3.fromRGB(255, 255, 255)
choiceButton1.Parent = choicesFrame
local choiceButton2 = Instance.new("TextButton")
choiceButton2.Size = UDim2.new(1, 0, 0.5, 0)
choiceButton2.Position = UDim2.new(0, 0, 0.5, 0)
choiceButton2.Text = "Option 2"
choiceButton2.TextScaled = true
choiceButton2.BackgroundTransparency = 0.5
choiceButton2.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
choiceButton2.TextColor3 = Color3.fromRGB(255, 255, 255)
choiceButton2.Parent = choicesFrame
-- Parent the dialogueGui to NPC's PrimaryPart
dialogueGui.Parent = npc.PrimaryPart
-- Function to start dialogue
local function StartDialogue()
dialogueGui.Enabled = true
PlayNPCAnimation()
PlayNPCSound()
end
-- Function to handle player touching the dialogue part
dialoguePart.Touched:Connect(function(hit)
local character = hit.Parent
if playerTouched == false and character and character:FindFirstChild("Humanoid") then
playerTouched = true
StartDialogue()
end
end)
-- Function to handle choices
local function HandleChoice(choice)
if choice == "Option1" then
-- Trigger next dialogue or action
elseif choice == "Option2" then
-- End dialogue or trigger different action
end
EndDialogue()
end
-- Function to end dialogue
local function EndDialogue()
dialogueGui.Enabled = false
playerTouched = false
end
-- Connect buttons to HandleChoice function
choiceButton1.MouseButton1Click:Connect(function()
HandleChoice("Option1")
end)
choiceButton2.MouseButton1Click:Connect(function()
HandleChoice("Option2")
end)
-- Function to play NPC animation
local function PlayNPCAnimation()
local animator = npc:FindFirstChildWhichIsA("Animator")
if animator then
local animation = script:WaitForChild("Animation") -- Place your animation in the script
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
end
end
-- Function to play NPC sound
local function PlayNPCSound()
local sound = npc:FindFirstChild("Sound") -- Place your sound in the NPC
if sound then
sound:Play()
end
end
-- Function to update GUI position to always face player
game:GetService("RunService").RenderStepped:Connect(function()
if dialogueGui.Enabled then
local playerCharacter = player.Character
if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") then
local playerPosition = playerCharacter.HumanoidRootPart.Position
dialogueGui.CFrame = CFrame.new(npc.PrimaryPart.Position, playerPosition)
end
end
end)