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

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

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

✓ Accepted answer

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.

1 comment

0

This worked, thank you so much!

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