Problems with numbering system

Hey there it’s me again. I’m making a numbering system for my train, here’s how its intended to work. By default there is a StringValue in a Values folder in the model called “UNIT_CLASS” this contains the locomotives class in this case: V43.

And then the UNIT_NUMBER inside the same folder. This contains the units actual number. This number ranges from 1000 to 1999. So basically how this is intended to work is that. The first of these series starts at 1000 of course. The game detects for any StringValues that have the value set to “431” if there is more than one locomotive model (lets say someone spawns it) that has this value set. Then it will cycle through the locomotive until it finds the appropriate textlabels to update update the UNIT_NUMBER by 1. So if there is already a V43 1000 it will become V43 1001, and so on until it reaches V43 1999.

However lets say someone with a V43 1944 deleted their train and theres a 1943 and 1945? Whoever spawns another one of this locomotive it will go for the missing number before updating to the next. So it will do 1944, fill in the gap and continue

BY THE WAY, the “431” thing I had mentioned is determed by a StringValue called "SPECIAL_CLASS

If 1999 is hit it will stop updating. However, whenever I AM testing out this new script it doesn’t seem to work. Here is my script

local valuesFolder = script.Parent.VALUES
local unitClass = valuesFolder:WaitForChild("UNIT_CLASS")
local unitNumber = valuesFolder:WaitForChild("UNIT_NUMBER")
local specialClass = valuesFolder:WaitForChild("SPECIAL_CLASS")

local function updateTrainNumber()
	local lastNumber = unitNumber.Value
	local newNumber = lastNumber + 1
	if newNumber > 1999 then
		newNumber = 1000
	end
	unitNumber.Value = newNumber
end

local function checkSpecialNumber()
	local specialNumbers = {}

	local stringValues = workspace:GetDescendants()
	for _, stringValue in ipairs(stringValues) do
		if stringValue:IsA("StringValue") and stringValue.Value == 431 then
			table.insert(specialNumbers, stringValue)
		end
	end

	if #specialNumbers > 1 then
		updateTrainNumber()
	end
end

local function updateTextLabels()
	local textLabelA = script.Parent.LOCOMOTIVE.MAIN_BODY:WaitForChild("FRONT_NUMTAG"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
	local textLabelV = script.Parent.LOCOMOTIVE.MAIN_BODY:WaitForChild("REAR_NUMTAG"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
	local sideLabelA = script.Parent.LOCOMOTIVE.MAIN_BODY.SIDE_NUMTAG_A.SurfaceGui.TextLabel
	local sideLabelV = script.Parent.LOCOMOTIVE.MAIN_BODY.SIDE_NUMTAG_B.SurfaceGui.TextLabel

	textLabelA.Text = unitClass.Value .. " " .. unitNumber.Value
	textLabelV.Text = unitClass.Value .. " " .. unitNumber.Value
	sideLabelA.Text = unitClass.Value .. " " .. unitNumber.Value
	sideLabelV.Text = unitClass.Value .. " " .. unitNumber.Value

end

updateTextLabels()

unitClass.Changed:Connect(updateTextLabels)
unitNumber.Changed:Connect(updateTextLabels)

I test it multiple times and it doesn’t work. It updates to 1000 and doesn’t change if theres another of the same thing there. So can anybody help? + How I can add the other criteria I mentioned? I tried at this point and some help would be very appreciated. I tried using the assistnat for help + ChatGPT becuase with myself who has his limits on scripting I’ve kind of given up

1 Like

if stringValue:IsA("StringValue") and stringValue.Value == 431 then

You are checking for a StringValue and then asking it contains a NumberValue.

Try changing the Number 431 to a String "431".

if stringValue:IsA("StringValue") and stringValue.Value == "431" then

I had changed that and yet it still didn’t work.

I was able to get everything to work with that one change.

But, I also called checkSpecialNumber().

I don’t see that you ever call that function.

You are only calling updateTextLabels() from a Changed event.

I played around with it more and simplified everything.

You may figure out what to do to your code by looking at the simplified version.

Here is the simplified file:

NumberTags.rbxl (67.9 KB)