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

Why do I have to call this function twice to take full effect?

Asked by 3 years ago

In the following script, whenever I call the function "updateClass" only the textLabel.Text value gets changed, therefore, I have to call the function twice (as shown in line 15) in order for the function to change both textLabel.Text and textLabel.BackgroundColor3. This feels counterintuitive and I was wondering if I'm doing something wrong or if this is as good as it can get. If additional clarification is needed, please don't hesitate to tell me. All help is appreciated!

local Players = game:GetService("Players")
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

local class = game.ReplicatedStorage:WaitForChild(player.Name):WaitForChild("Class")
local classColor = game.ReplicatedStorage:WaitForChild(player.Name):WaitForChild("CColor")
local textLabel = script.Parent

local function updateClass()
    textLabel.Text = class.Value
    textLabel.BackgroundColor3 = classColor.Value
end

updateClass()
class.Changed:Connect(updateClass)
classColor.Changed:Connect(updateClass)

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

You're listening for two separate values, and therefore you can wait for the other property to change if you'd like instead of creating another listener connected to the function:

local function updateClass()
    classColor.Changed:Wait() -- Yields until classColor changes
    -- Something else here
end

If one value changes, the function fires and will boldly assume that both values have changed. According to your information, the color value doesn't change when the Text changes.

0
This worked, thank you so much! ChirpPerson 74 — 3y
Ad

Answer this question