How i do combine 5 numbers?

Hey, What i’m trying to do is a random generated code on a text label then set a attribute in a door. but the problem is that every number is separated so i only get one digit… i tried to google and i don’t see something that helps me here my code:

local Code1 = math.random(1,9)
local Code2 = math.random(1,9)
local Code3 = math.random(1,9)
local Code4 = math.random(1,9)

local CodeFolder = workspace.CodeParts
local DoorCode = workspace.Door.DoorCode



local Code = {
	
	Digit1 = tostring(Code1),
	Digit2 = tostring(Code2),
	Digit3 = tostring(Code3),
	Digit4 = tostring(Code4)
	


}



for i,v in pairs(CodeFolder:GetChildren()) do
	if v:IsA("Part") then
	    GeneratedCode = math.random(#Code, 9)

	
		v.SurfaceGui.CodeText.Text = GeneratedCode
		

		print(GeneratedCode)

	end
end


DoorCode:SetAttribute("DoorCode", GeneratedCode)
1 Like

Just have an empty string and then go through a loop and concetenate all of the numbers add them ont the empty string

1 Like
-- Empty string
code = ""

-- Loop 9 times, from 1 - 10; change 10 to how long you want your code to be
for i = 1, 10, 1 do
    
    -- Concatenate the current value of code with a random number
    code = code .. tostring(math.random(1, 9))
end

-- Cast the code to an int
code = tonumber(code)

-- Set the code attribute of the object to the generated code
obj:SetAttribute("Code", code)
1 Like

still i don’t understand can you give a example?

I know this topic already has been solved, but I believe there’s another approach to the problem.

local Digit1 = math.random(1,9) * 1000
local Digit2 = math.random(0,9) * 100
local Digit3 = math.random(0,9) * 10
local Digit4 = math.random(0,9)
local Code = Digit1 + Digit2 + Digit3 + Digit4

sets randomized digits, and then adds them all up to create a 4 digits code

the code above came up to my mind first, obviously it should be a loop to set a number for every digit but you get the point

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.