Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does anyone know a more efficient, working way of using this custom dialog?

Asked by
souflee 50
4 years ago

Hey there,

I'm working on a dialog system where a player can choose between options to say to an NPC, once an option is chosen, M should be pressed to continue the dialog and see the NPC's answer. However, the code (below) I wrote for it doesn't work, and I feel that it's very inefficient to keep calling a user input function every time an option is chosen.

Any help would be greatly appreciated. Thanks in advance!

local frame = script.Parent
local userId = game.Players.LocalPlayer.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

local player = game.Players.LocalPlayer

frame.Option1.MouseButton1Down:Connect(function()
    frame.ThumbnailPlayer.Image = content
    frame.ThumbnailPlayer.Visible = true
    frame.Dialog.Text = "Greetings! My name is " .. player.Name .. "."
    frame.NameTxt.Text = game.Players.LocalPlayer.Name

    frame.Continue.Visible = true
        local function onKeyPress(inputObject, gameProcessedEvent)
               if inputObject.KeyCode == Enum.KeyCode.M then
            frame.ThumbnailPlayer.Visible = false
            frame.NameTxt.Text = frame.SpeakerName.Value
            frame.Dialog.Text = "Good day to you, " .. player.Name .. ". I am Sir Bram."
        end
            game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

    end
end)

-----

frame.Option2.MouseButton1Down:Connect(function()
    frame.ThumbnailPlayer.Image = content
    frame.ThumbnailPlayer.Visible = true
    frame.Dialog.Text = "Do you have anything to do for me?"
    frame.NameTxt.Text = game.Players.LocalPlayer.Name

    frame.Continue.Visible = true
        local function onKeyPress(inputObject, gameProcessedEvent)
               if inputObject.KeyCode == Enum.KeyCode.M then
            frame.ThumbnailPlayer.Visible = false
            frame.NameTxt.Text = frame.SpeakerName.Value
            frame.Dialog.Text = "I'm afraid I don't."
        end
    end
        game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
end)

-----

frame.Option3.MouseButton1Down:Connect(function()
    frame.ThumbnailPlayer.Image = content
    frame.ThumbnailPlayer.Visible = true
    frame.Dialog.Text = "I should be heading off now."
    frame.NameTxt.Text = game.Players.LocalPlayer.Name

    frame.Continue.Visible = true
        local function onKeyPress(inputObject, gameProcessedEvent)
               if inputObject.KeyCode == Enum.KeyCode.M then
            frame.ThumbnailPlayer.Visible = false
            frame.NameTxt.Text = frame.SpeakerName.Value
            frame.Dialog.Text = "Farewell, " .. game.Players.LocalPlayer.Name .. "."
        end
    end
        game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
    end)

Answer this question