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

Textlabel doesn't change via scripts?

Asked by 8 years ago

Hi, I have been creating a script recently and have been trying to add to it to help myself learn, though I have recently come across a problem that I cannot work out.

The problem is that I have created a textlabel in my gui and entered some text into it via the properties menu. I then went into my script and I intended to change the text if a random number is below or above 50, though what actually happens is that the text in the gui doesn’t change at all

Here’s the script,

a = 0

money = game.Workspace.money
maintextgui = game.StarterGui.ScreenGui.Frame.MainTextGui.Text

wait(5)

math.randomseed(tick())
for _ = 1, 1 do
a = math.random(100)
print(a)

if a <= 50 then
print("Below 50!")
money.Value = money.Value - 1
maintextgui = "Below 50"
else
print("Above 50!")
money.Value = money.Value + 1
maintextgui = "Above 50!"
end
end

Could I please have help in solving this please?

Just a note, the money part relates to an intvalue, which I’m assuming doesn’t relate to the problem, so you can ignore that part, just didn’t want to cause more problems by deleting it and possibly causing a misunderstanding

Thanks!

0
you are changing the value of a variable, not the text itself. remove the last .Text from your variable, and put those in when changing the text TheDeadlyPanther 2460 — 8y
0
So like maintextgui.Text instead? I've just tried that and it isn't working lookathis 0 — 8y
0
Make sure you're using a LocalScript. ServerScripts do not work for GUIs. SimplyRekt 413 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

You have two problems:

1. The one I mentioned in the comments, hopefully you fixed that. Just for reference if you didn't get it right:

local label = gui.TextLabel
label.Text = "Hey!"

2. StarterGui does not change the player's gui until they respawn. this is because StarterGui is where stuff gets cloned from. If you want to do it, you need to index the player so you can get into their PlayerGui. You can do this with LocalScripts:

local p = game.Players.LocalPlayer
local gui = p:PlayerGui:FindFirstChild("ScreenGui")

if gui then
    gui.TextLabel.Text = "Hello!"
end

Or with a normal Script:

game.Players.PlayerAdded:connect(function(p)
    p.CharacterAdded:connect(function()
        local gui = p:PlayerGui:FindFirstChild("ScreenGui")
        if gui then
            gui.TextLabel.Text = "Hello!"
        end
    end)
end)
Ad

Answer this question