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

My script wont work through variables?

Asked by
bowanw 16
7 years ago

I know that the 'Hint' class is deprecated, but I wanted to test making a countdown.

local Hint = Instance.new("Hint")
Hint.Parent = workspace
local Text = Hint.Text


Text = "5"
while Text > 0 do
    wait(1)
    Text = Text-1
end -- This code doesn't work, I even tried converting to numbers and to strings

I noticed that I could fix it from instead of using a variable for Text, I edit it directly, why is this?

local Hint = Instance.new("Hint")
Hint.Parent = workspace


workspace.Message.Text = 5
while tonumber(workspace.Message.Text) > 0 do
    wait(1)
    workspace.Message.Text = workspace.Message.Text -1
    print(workspace.Message.Text)
end --This code does work
0
You cannot assign a property of an instance to a variable. unmiss 337 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You cannot change a property by referencing it from a variable because when you assign a variable to a property, the variable becomes a static representation of the property's value at the time the script executed the definition.

So, when you go to set the variable again, you are just changing the variable.. not the property.

This is why you must always index the Property from it's parent to actually change it.

--Example
local part = workspace.Part;
local color = part.BrickColor

print(color); --> Medium stone grey
color = BrickColor.new("Really red");
print(part.BrickColor); --> Medium stone grey
print(color); --> Really red
Ad

Answer this question