How do I use randomly generated Numbers as Strings

I have this keypad code door that I want to have a random code each time you play, but when I test it, it seems to not think it is correct. I assume it’s because it’s a number value and not a string. I have tried to use quote marks on the numbers to turn it into a string, But it didn’t work when I tried the code and entered it. Is there a way to make a randomly generated number and turn it into a string value so the script can properly work

That seems to be the only issue with this, When I put in a regular number in quotes like this:
local Code = “1337”
then it seems to recognise it as a code to use. But when I use math.random to get a number, it won’t register as a valid code because it is automatically classed as a number value, and not a string.

local Door = script.Parent.CodeDoor
local input = script.Parent.Display.Display
local Indicator = script.Parent.Display.Indicator
local buttons = script.Parent.Buttons

local Code = math.random(1000,9999) -- The problem here is this is number value
print(Code)
local CurrentCode = ""

input.SurfaceGui.TextLabel.Text = CurrentCode

for i, button in pairs(buttons:GetChildren()) do
	
	
	button.ClickDetector.MouseClick:Connect(function()
		if tonumber(button.Name) then
			
			button:WaitForChild("Click"):Play()
			CurrentCode = CurrentCode .. button.Name
			input.SurfaceGui.TextLabel.Text = CurrentCode
			
			
			elseif button.Name == "Clear" then
			
			CurrentCode = nil
			input.SurfaceGui.TextLabel.Text = CurrentCode
			
			elseif button.Name == "Enter" then
				
				if CurrentCode == Code then
					
					input.Correct:Play()
					CurrentCode = ""
					Indicator.Material = "Neon"
					Indicator.BrickColor = BrickColor.new("Lime green")
					
					input.SurfaceGui.TextLabel.Text = "Correct Code, Security Room Unlocked"
					Door:SetAttribute("Door", true)
					Door.Green.PathfindingModifier.PassThrough = true
					script.Enabled = false
					
				else
					
					CurrentCode = ""
					input.SurfaceGui.TextLabel.Text = "Wrong Code"
					
					Indicator.Material = "Neon"
					Indicator.BrickColor = BrickColor.new("Really red")
					wait(3)
					Indicator.BrickColor = BrickColor.new("Black")
					Indicator.Material = "Plastic"
				end
			
		
		end
	end)
end

Please help, I would appreciate any help with this.

1 Like

Thankfully, a built-in function exists to convert a number to a string. See here.

2 Likes

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