How can I make a TextLabel have its text synchronize as assigned to an IntValue?

I have finished my Ro-Scale Clyde G12 from the Kowloon-Canton Railway. There are five of them. This gave me the idea to include the feature of assigning a name to each DCC address; the locomotive numbers (DCC addresses) and respective names are shown below:
51. Sir Alexander
52. Lady Maurine
53. H.P. Winslow
54. R. Baker
55. R.D. Walker

I had scripted the names to correspond to the DCC address of the locomotive, but every time I leave a locomotive having the same DCC address as another, the names do not change. Here is what I had coded so far:

if script.Parent.Parent.DCCProperties.DCCAddress==51 then
	script.Parent.Left.CabNameLabel.Text="SIR ALEXANDER"
	script.Parent.Right.CabNameLabel.Text="SIR ALEXANDER"
else if script.Parent.Parent.DCCProperties.DCCAddress==52 then
		script.Parent.Left.CabNameLabel.Text="LADY MAURINE"
		script.Parent.Right.CabNameLabel.Text="LADY MAURINE"
	else if script.Parent.Parent.DCCProperties.DCCAddress==53 then
			script.Parent.Left.CabNameLabel.Text="H.P. WINSLOW"
			script.Parent.Right.CabNameLabel.Text="H.P. WINSLOW"
		else if script.Parent.Parent.DCCProperties.DCCAddress==54 then
				script.Parent.Left.CabNameLabel.Text="R. BAKER"
				script.Parent.Right.CabNameLabel.Text="R. BAKER"
			else if script.Parent.Parent.DCCProperties.DCCAddress==55 then
					script.Parent.Left.CabNameLabel.Text="R.D. WALKER"
					script.Parent.Right.CabNameLabel.Text="R.D. WALKER"
				end
			end
		end
	end
end

The DCC address is an IntValue that gives a locomotive its number, as @NWSpacek said in the Ro-Scale Trains Wiki. I experimented with locomotive names as above, but the names did not change at all. The question is: What can make the names changing along with an IntValue possible?

if script.Parent.Parent.DCCProperties.DCCAddress==51 then
	script.Parent.Left.CabNameLabel.Text="SIR ALEXANDER"
	script.Parent.Right.CabNameLabel.Text="SIR ALEXANDER"
elseif script.Parent.Parent.DCCProperties.DCCAddress==52 then
	script.Parent.Left.CabNameLabel.Text="LADY MAURINE"
	script.Parent.Right.CabNameLabel.Text="LADY MAURINE"
elseif script.Parent.Parent.DCCProperties.DCCAddress==53 then
	script.Parent.Left.CabNameLabel.Text="H.P. WINSLOW"
	script.Parent.Right.CabNameLabel.Text="H.P. WINSLOW"
elseif script.Parent.Parent.DCCProperties.DCCAddress==54 then
	script.Parent.Left.CabNameLabel.Text="R. BAKER"
	script.Parent.Right.CabNameLabel.Text="R. BAKER"
elseif script.Parent.Parent.DCCProperties.DCCAddress==55 then
	script.Parent.Left.CabNameLabel.Text="R.D. WALKER"
	script.Parent.Right.CabNameLabel.Text="R.D. WALKER"
end

Fixed the indentation and replaced “else if” with “elseif”.

Got that! I’ll try to see if it works.

Is this the entire script? I’m assuming there’s more.

You need a .Changed function. With that, everytime your value changes, you can make it do what it is supposed to do. Because right now, you script checks once if the value is XX number, with .Changed function, everytime your value changes, the script will check each of these times.

https://developer.roblox.com/en-us/api-reference/event/Instance/Changed

1 Like

It is. Unfortunately, it doesn’t work. All it does is set the DCC address to 53.

“DCCAddress” what type of instance is this?

1 Like
local DCCProperties = script.Parent.Parent:WaitForChild("DCCProperties")
local DCCAddress = DCCProperties:WaitForChild("DCCAddress")

DCCAddress.Changed:Connect(function(newVal)
	if newVal==51 then
		script.Parent.Left.CabNameLabel.Text="SIR ALEXANDER"
		script.Parent.Right.CabNameLabel.Text="SIR ALEXANDER"
	elseif newVal==52 then
		script.Parent.Left.CabNameLabel.Text="LADY MAURINE"
		script.Parent.Right.CabNameLabel.Text="LADY MAURINE"
	elseif newVal==53 then
		script.Parent.Left.CabNameLabel.Text="H.P. WINSLOW"
		script.Parent.Right.CabNameLabel.Text="H.P. WINSLOW"
	elseif newVal==54 then
		script.Parent.Left.CabNameLabel.Text="R. BAKER"
		script.Parent.Right.CabNameLabel.Text="R. BAKER"
	elseif newVal==55 then
		script.Parent.Left.CabNameLabel.Text="R.D. WALKER"
		script.Parent.Right.CabNameLabel.Text="R.D. WALKER"
	end
end)

As the above post mentions you should use a “Changed” event like here to detect value changes, this will only work if “DCCAddress” is an “IntValue” instance or a “NumberValue” instance however.

1 Like

Thanks for the help! It finally works.

No problem, glad to have helped.

1 Like