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

How do I properly set text of a GUI with a function?

Asked by 6 years ago
Edited 6 years ago

I am attempting to make a power level system for my ship, and I wanted a GUI to say the exact power level. I started off with something very basic, or at least I thought it was basic. The text of the GUI does not set in the function. However, if I move the setText outside of the function, the GUI updates. When the function runs, the power value does increase, but the GUI text is not changed. Any ideas?

power = script.Parent.Power
gui = game.StarterGui.ScreenGui.TextBox
function powerIncrease()
    power.Value = power.Value + 1   
    gui.Text = "Power level: "..power.Value
end
script.Parent.Add.ClickDetector.MouseClick:connect(powerIncrease)

vs this:

power = script.Parent.Power
gui = game.StarterGui.ScreenGui.TextBox
function powerIncrease()
    power.Value = power.Value + 1
end
gui.Text = "Power level: "..power.Value
script.Parent.Add.ClickDetector.MouseClick:connect(powerIncrease)
0
The thing is that the text will only update when you call the powerIncrease function, so when you first start up, since you haven't pressed the ClickDetector, it won't update the text. User#8349 0 — 6y
0
Hi, thank you for your reply. I forgot to specify. I am running the function. The power level increases every time. But the text of the GUI still doesn't change. The function completes, but skips over the GUI text. Djmaster444 5 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

You're trying to display text on the regular Gui and not the one native to the player(s). Try this:

power = script.Parent.Power
function powerIncrease()
    for _,i in pairs(game.Players:GetPlayers()) do
        power.Value = power.Value + 1   
        i.PlayerGui.ScreenGui.TextBox.Text = "Power level: "..power.Value
    end
end
script.Parent.Add.ClickDetector.MouseClick:connect(powerIncrease)

0
This man knows what's up ^^^^. When you update Guis, verify it isn't the one in StarterGui, and one inside a player. Conmmander 479 — 6y
0
you right DeceptiveCaster 3761 — 6y
0
I already explained that xD DeceptiveCaster 3761 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

What you chould do is, put a localscript into the StarterGui.ScreenGui.Textbox that goes like this

local textbox = script.Parent
local power = --this should lead to wherever your power object is

while wait() do
    textbox.Text = "Power lvl: "..power.Value
end

That will keep the text updated according to what the powerlevel is So now you can just worry about updating the value of the power in the other script.

0
Dude. You forgot about the PlayerGui, idiot. DeceptiveCaster 3761 — 6y
0
Your comment just shows how little you actually know. PoePoeCannon 519 — 6y

Answer this question