Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't my script with IF and THEN work?

Hey, I really need your help, I can't seem to make the script to work.


local speedRunWeather = game.ServerStorage.speedRunWeather local t = game.StarterGui.changeGUI.Text

if t.Text "Hey, this is a text." then speedRunWeather.Parent = game.Lightning



I would really appreciate if you guys could help me, thank you!

(filtering enabled, i put the script in serverscriptserver)

2 answers

✓ Accepted answer

an if statement should be formatted like this

if foo == bar then
print('Hello World!')
end

EDIT:

so your code should be like this.

local speedRunWeather = game.ServerStorage.speedRunWeather
local t = game.StarterGui.changeGUI

if t.Text == "Hey, this is a text." then
speedRunWeather.Parent = game.Lightning
end

It also should be noted that when changing text on a gui, don't save the text as a variable,

Variables in RLua have many types (ex. Bool, String, Instance, etc.)

When you save a variable like you did before

Reference:

local t = game.StarterGui.changeGUI.Text

It saves the Text property of the Instance as a String variable. This means that when you change it later in your code, it isn't changing the text that is on the TextLabel, it is changing the Text variable.

Example:

local textLabel = Instance.new('TextLabel')
textLabel.Text = 'foo'

local Text = textLabel.Text -- The variable "Text" now equals 'foo'

print(Text) -- Prints 'foo'

textLabel.Text = 'bar' -- We set the TextLabel's text to 'bar'

print(Text) -- Still prints 'foo'

The way to do it correctly is to save the Text variable as the TextLabel's Instance. (Or just reference the TextLabel directly every time)

Example:

local textLabel = Gui.Notification
textLabel.Text = 'foo'

-- or just do

Gui.Notification.Text = 'foo'

Doing this allows you to change the instance's Text property directly, instead of what you did before (saving the text property as a variable).

6 comments

0

No, He needs to change the text in the playergui not the startergui.

0

No, he said clearly that the script is in ServerScriptService

0

Any change in the startergui when the game is running wont replicate to the playergui.

0

He isn't changing the startergui, he is checking if an already present textlabel in the startergui has a certain text on it, if it does, he changes the lighting

0

lol sorry my bad.

0

mm this is compatible to filtering enabled right..

BuzzKillGT for line 4 you did:

if t.Text "Hey, this is a text."

you forgot to add a equal sign, so it didnt work -

if t.Text == ("Hey, this is a text.") then

and dont use use variables to store text, because if u change the text it doesnt update it, so use it directly(idk how to say it lol).

4 comments

0

Hehehe, I'll try your solution tommorow.

0

k

0

You forgot to put the "end".

0

ik, i was giving an example, plus roblox would automatically put an end anyways, do u think that im that stupid?

This discussion is preserved as it was. New answers, comments, and votes are closed.