Any way to change text created by a module?

ok so basically
im making a module where it creates a text for game purposes of course
and i need to change a text created from that module by using a other script
(if you dont understand what i said just uh ask for me to explain it again i guess)

the module
local ChapterText = {}

function ChapterText.ClassicChapterText()
	local ScreenGui = Instance.new("ScreenGui")
	local CText = Instance.new("TextLabel")
	ScreenGui.Parent = game.StarterGui
	CText.Parent = ScreenGui
	CText.Text = "Chapter #"
end

return ChapterText

so yeah
how can i do this? (and yes i know i have to require it or whatever)

Parameterize your function so that it can take a string and set the text to that.

function ChapterText.ClassicChapterText(Text: string)
	local ScreenGui = Instance.new("ScreenGui")
	local CText = Instance.new("TextLabel")
	ScreenGui.Parent = game.StarterGui
	CText.Parent = ScreenGui
	CText.Text = Text
end
2 Likes

but somehow it doesnt detect ctext?
image

I fixed your script a little bit, here are some examples of what you can do.

First, you can make this function and leave it as it is,

local ChapterText = {}

function ChapterText.ClassicChapterText()
	local ScreenGui = Instance.new("ScreenGui")
    ScreenGui.Name = "YourScreenGuiName"
	local CText = Instance.new("TextLabel")
    CText.Text = "Chapter #"
	ScreenGui.Parent = game.Players.LocalPlayer.PlayerGui
	CText.Parent = ScreenGui
end

return ChapterText

and in the other script(must be local)

local gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("YourScreenGuiName")
if gui then
   if gui:FindFirstChild("TextLabel") then
       gui:FindFirstChild("TextLabel").Text = "YourTextHere"
   end
end

You can basically change the text like this after you can create the text. (Another example is in replies)

1 Like

text.ClassicChapterText is the function it looks like. You might want to return the ScreenGui so you can then access the text label. I am also not sure why you are inserting it to StarterGui, it won’t show to players until after respawn.

local Players = game:GetService("Players")

local client = Players.LocalPlayer

function ChapterText.ClassicChapterText(Text: string)
	local ScreenGui = Instance.new("ScreenGui")
	local CText = Instance.new("TextLabel")
	ScreenGui.Parent = client.PlayerGui
	CText.Parent = ScreenGui
	CText.Text = Text

	return ScreenGui
end

Thenn in your script you can do

local gui = text.ClassicChapterText("Chapter #423423423")

and uh, that is it. Since the function sets it for you.

1 Like

That would create text labels over and over again. He wants to set the text after it has been created.

1 Like

That is to create a new one, which it looked like what OP wanted, hence why they create a new one each time.

i mean
yeah
otherwise i wouldnt create the module, its to reuse it again and again (which im gonna add a destroy to it for reusing it)

Here’s another example of what you can do.

local ChapterText = {}
local textcreated = false

local function CreateText(Text: string) -- This function will run if no gui has been created yet.
	local ScreenGui = Instance.new("ScreenGui")
    ScreenGui.Name = "TextGui"
	local CText = Instance.new("TextLabel")
    CText.Text = Text
	ScreenGui.Parent = game.Players.LocalPlayer.PlayerGui
	CText.Parent = ScreenGui
    textcreated = true -- Sets the textcreated to true so it won't create another gui again.
end

function ChapterText.ClassicChapterText(Text: string)
    if textcreated == false then -- Checks if there's already a gui (If the function above did not run)
       CreateText(Text) -- Creates a new gui with the text
    else -- If there's already a gui
        local gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("TextGui") -- Finds the gui
        if gui then
           if gui:FindFirstChild("TextLabel") then -- Finds the textlabel
               gui:FindFirstChild("TextLabel").Text = Text -- Updates the text of the textlabel to the new text.
           end
        end
    end
end
return ChapterText

but wouldnt that be just reusing the same module (copying it) again and again for reusing stuff?
the module only needs to stay as one

Uh no? You can just require the module and run the function, and it’ll do the job for you.
(If it already created a GUI, instead of creating a new one, it finds the GUI that’s been already created and changes the text of the TextLabel.)

but then again
that example you gave me is way too hard for me to understand whats going on, since im not that good at scripting.

Let me edit it so you’ll understand.

no actually there is no need, @sjr04 already gave a solution

Aight then, Idk if it’ll lag your game or not since it creates new guis but ok

1 Like

it actually wont!
again im going to make the gui destroy itself after its done, so it wont lag!

1 Like