Everything works except changing text - hep would be greatly appreciated!

Hey there,
I’m currently making a homemade dialog system where if you press F after clicking a text option, the dialog will continue (if you keep pressing F more dialog will be revealed).

This works perfectly fine if I try to, for example, fire an event or print something. But if I want to change the dialog text it simply refuses to work - no errors are given either.

I’ve tried changing the variable from “frame.Dialog.text” to “script.Parent.Dialog.Text”, looked for solutions online ect. to no avail.

Help would be greatly appreciated! You can find a snippet of the code I’m on about below:

-- note to self: incase dialog should be longer, simply add multiple onKeyPress functions.

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

local path1activated = false
local path2activated = false
local path3activated = false

frame.Option1.MouseButton1Down:Connect(function()
		
	frame.Option1.BackgroundColor3 = Color3.fromRGB(99, 186, 49)
		
	frame.ThumbnailPlayer.Image = content
	frame.ThumbnailPlayer.Visible = true
	frame.ThumbnailSpeaker.Visible = false
	frame.Dialog.Text = "Good day! May I have a look at your stock?"
	frame.NameTxt.Text = game.Players.LocalPlayer.Name
	
		if path1activated == false then
		path1activated = true
		path2activated = false
		path3activated = false
	
	frame.Continue.Visible = true
		local function onKeyPress(inputObject, gameProcessedEvent)
			  if inputObject.KeyCode == Enum.KeyCode.F
			and path1activated == true then
				frame.ThumbnailPlayer.Visible = false
				frame.ThumbnailSpeaker.Visible = true
				frame.NameTxt.Text = frame.SpeakerName.Value
				frame.Dialog.Text = "Ofcourse ye may - have a gander..."
			-----
		local function onKeyPress(inputObject, gameProcessedEvent)
			if inputObject.KeyCode == Enum.KeyCode.F
					and path1activated == true then
						frame.Dialog.Text = "template text"
						print("this works")
					--- add store gui script			
						
				end
			end
		game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
end
		end
		game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Does any text get displayed or none at all?

Hey there, the text simply doesn’t change from “Ofcourse ye may - have a gander…”
to “template text”.

I presume both onKeyPress function ran, but the second one ran before the first one, try printing something in the first function to confirm this. I’d make a boolean to check if the first one ran, if you want to run it once (or use :Disconnect).

Also changing from from.Dialog.text to script.Parent.Dialog.Text doesn’t change anything as Instances are passed by reference.

A nitpick but I’d avoid == true, and == false, and use if path1activated then and if not path1activated then. Unless in cases where you want values that’s not false and that evaluates to false (like 0, 0n, NaN, "", null and undefined in JavaScript and nil in Lua) to not run (which is unlikely as I assume path1activated is not going to become nil), using == false and == true is unessesary.

1 Like

Putting two of the same if statements (in simple terms) confuses the script on which one to choose. A very basic way to solve this is to use if statements. So on key press, if the text is already “of course ye may - have a gander…” then change to “template text”.

local function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F and path1activated == true then
         if frame.Dialog.Text == "Ofcourse ye may - have a gander..." then
            frame.Dialog.Text = "template text"
         end
    end
end

Of course, this is the simplest way to solve the problem. More complicated ways involve the use of tables where you put the text in sequence and add one to the index to get the next text.

TableofDialogue = {"Ofcourse ye may - have a gander...", "template text"}
local NumberofFKeyPressed = 0 -- Set to zero at the start since you have not pressed at all
local function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F and path1activated == true then
         NumberofFKeyPressed = NumberofFKeyPressed + 1 -- On F key press, count increases by 1
         frame.Dialog.Text = TableofDialogue[NumberofFKeyPressed] -- table[index] gets the text in the place that you want
    end
end
2 Likes

Thanks for the help, lads and lassies! Both of your suggestions seem very viable, but in the end I went with @AbandonedRick’s solution. Very clever, I must say! If there’s any way I can show appreciation to the two of you, just let me know!

1 Like