How to get how many digits + error

So basically, there is a error within this part in my hotel system:

local StringLen = math.ceil(math.log10(GenString))

The GenString was made using this function

game.ReplicatedStorage.BookRE.OnServerEvent:Connect(function(PlayerV, CheckedIn)
	local Player = game.Players:FindFirstChild(PlayerV)
	if Player then
		if not game.ReplicatedStorage.Rooms:FindFirstChild(Player.Name) then
			for i,v in pairs(workspace.Doors:GetChildren()) do
				local Children1 = workspace.Doors
				local Count = #Children1:GetChildren()
				if Count >= 10 then
					local Count2 = i
					GenString = math.random(1, Count2)
				elseif Count >= 100 then
					local Count3 = i
					GenString = math.random(1, Count3)
				elseif Count >= 1 then
					local Count4 = Count
					GenString = math.random(1, Count4)
				end
			end
		end
	end
end)

What I want to do is to be able to check how many digits is in the StringLen variable. [ [ The GenString variable value is local StringLen = math.ceil(math.log10(GenString)) ]

So, for an example how I would like to use this function is

	if StringLen >= 1 and not game.ReplicatedStorage.AlreadyChecked:FindFirstChild("00" .. GenString) then

Full Code:

local GenString = nil
local AddString = nil

game.ReplicatedStorage.BookRE.OnServerEvent:Connect(function(PlayerV, CheckedIn)
	local Player = game.Players:FindFirstChild(PlayerV)
	if Player then
		if not game.ReplicatedStorage.Rooms:FindFirstChild(Player.Name) then
			for i,v in pairs(workspace.Doors:GetChildren()) do
				local Children1 = workspace.Doors
				local Count = #Children1:GetChildren()
				if Count >= 10 then
					local Count2 = i
					GenString = math.random(1, Count2)
				elseif Count >= 100 then
					local Count3 = i
					GenString = math.random(1, Count3)
				elseif Count >= 1 then
					local Count4 = Count
					GenString = math.random(1, Count4)
				end
			end
		end
	end
	
	-- local count = 0
	local StringLen = math.ceil(math.log10(GenString))
	--[[for i,v in pairs(workspace.Doors) do
		count = count + 1
	end--]]
	if StringLen >= 1 and not game.ReplicatedStorage.AlreadyChecked:FindFirstChild("00" .. GenString) then
		local KeyCard = Instance.new("Tool")
		KeyCard.Name = "00" .. GenString
		KeyCard.CanBeDropped = false
		local Handle = script.Handle:Clone()
		Handle.Parent = KeyCard
		KeyCard.Parent = Player.Backpack
		local RoomSecure = Instance.new("NumberValue")
		RoomSecure.Parent = game.ReplicatedStorage.Rooms
		RoomSecure.Name = Player.Name
		RoomSecure.Value = "00" .. GenString
		RoomSecure.Name = Player.Name
		local RoomSecure2 = Instance.new("NumberValue")
		RoomSecure2.Parent = game.ReplicatedStorage.AlreadyChecked
		RoomSecure2.Name = "00" .. GenString
		RoomSecure2.Value = Player.Name
		GenString = nil
	elseif StringLen >= 10 and not game.ReplicatedStorage.AlreadyChecked:FindFirstChild("0" .. GenString) then
		local KeyCard = Instance.new("Tool")
		KeyCard.Name = "0" .. GenString
		KeyCard.CanBeDropped = false
		local Handle = script.Handle:Clone()
		Handle.Parent = KeyCard
		KeyCard.Parent = Player.Backpack
		local RoomSecure = Instance.new("NumberValue")
		RoomSecure.Parent = game.ReplicatedStorage.Rooms
		RoomSecure.Name = Player.Name
		RoomSecure.Value = "0" .. GenString
		RoomSecure.Name = Player.Name
		local RoomSecure2 = Instance.new("NumberValue")
		RoomSecure2.Parent = game.ReplicatedStorage.AlreadyChecked
		RoomSecure2.Name = "0" .. GenString
		RoomSecure2.Value = Player.Name
		GenString = nil
	elseif StringLen >= 100 and not game.ReplicatedStorage.AlreadyChecked:FindFirstChild(GenString) then
		local KeyCard = Instance.new("Tool")
		KeyCard.Name = GenString
		KeyCard.CanBeDropped = false
		local Handle = script.Handle:Clone()
		Handle.Parent = KeyCard
		KeyCard.Parent = Player.Backpack
		local RoomSecure = Instance.new("NumberValue")
		RoomSecure.Parent = game.ReplicatedStorage.Rooms
		RoomSecure.Name = Player.Name
		RoomSecure.Value = GenString
		local RoomSecure2 = Instance.new("NumberValue")
		RoomSecure2.Parent = game.ReplicatedStorage.AlreadyChecked
		RoomSecure2.Name = GenString
		RoomSecure2.Value = Player.Name
		GenString = nil
	end
end)


game.Players.PlayerRemoving:Connect(function(Player)
	if game.ReplicatedStorage.Rooms:FindFirstChild(Player.Name) then
		game.ReplicatedStorage:WaitForChild(Player.Name):Destroy()
	end
end)

game.ReplicatedStorage.UnBookRE.OnServerEvent:Connect(function(PlayerEV)
	local PlayerV = game.Players:GetUserIdFromNameAsync(PlayerEV)
	local Player = game.Players:GetPlayerByUserId(PlayerV)
	local CloneE = script.Unbook:Clone()
	CloneE.Parent = Player.PlayerGui
end)

game.ReplicatedStorage.UnBookConfirmRE.OnServerEvent:Connect(function(Player)
	if game.ReplicatedStorage.Rooms:FindFirstChild(Player.Name) then
		Player.Backpack:WaitForChild(game.ReplicatedStorage.Rooms:WaitForChild(Player.Name).Value):Destroy()
		game.ReplicatedStorage.AlreadyChecked:WaitForChild(game.ReplicatedStorage.Rooms:WaitForChild(Player.Name).Value):Destroy()
		game.ReplicatedStorage.Rooms:WaitForChild(Player.Name):Destroy()
	end
end)

Theres no Count2,Count3 and Count4 variable.

Also PLEASE describe what the error is, otherwise you have to figure it out by yourself.

Yes there is:

local Count = #Children1:GetChildren()
				if Count >= 10 then
					local Count2 = i
					GenString = math.random(1, Count2)
				elseif Count >= 100 then
					local Count3 = i
					GenString = math.random(1, Count3)
				elseif Count >= 1 then
					local Count4 = Count
					GenString = math.random(1, Count4)
				end

Okay but you still haven’t described the error???

You are currently viewing the RoomHandler script, which I created for a hotel system. It will randomly select items from this order based on how many things are in the Rooms Folder, which will be detected by how many objects are in a folder inside Workspace:

1 - 9 Objects: 00 - RandomizedStringVariable
10 - 99 Objects: 0 - RandomizedStringVariable
100+ Objects: RandomizedStringVariable

The randomised number will then be added to the GenString variable. The GenString variable will eventually be used to retrieve text, but it will also be used to retrieve the amount of digits in the randomised integer for another variable. It will perform this within an if statement after checking to see whether it is greater than a certain number. I made an effort to explain everything as clearly as I could.

So, an example for how the StringLen shall be used:
if StringLen >= 1 then
– Which in this case means that the GenString variable has over 1 character.

I assume (based on the fact this logic works perfectly otherwise) the error only occurs with exact multiples of 10, and the number 1 (as your GenString result)

Here’s your fix, just put it somewhere in-between setting your GenString variable and getting your StringLen variable:

if (GenString / 10) % 1 == 0 or GenString == 1 then
	GenString += 1
end

This only happens because log10 is, well, the logarithmic value of 10.

10^1 = 10 so log10(10) = 1.

10^2 = 100 so log10(100) = 2.

You get the jist.

log10(1) becomes 0, because well… 10^0 == 1

1 Like
game.ReplicatedStorage.BookRE.OnServerEvent:Connect(function(PlayerV, CheckedIn)
	local Player = game.Players:FindFirstChild(PlayerV)
	if Player then
		if not game.ReplicatedStorage.Rooms:FindFirstChild(Player.Name) then
			for i,v in pairs(workspace.Doors:GetChildren()) do
				local Children1 = workspace.Doors
				local Count = #Children1:GetChildren()
				if (GenString / 10) % 1 == 0 or GenString == 1 then
					GenString += 1
				end
			end
		end
	end

Like this, or what?

No, you still need to get GenString in the first place, that’s what your math.random logic was for, no?

The code I provided is for fixing the value of GenString if it’ll result in an incorrect value.

So put it outside of the loop, and add your math.random() logic back to the loop.

Ideally, you’d add it where I’ve marked with a comment:

1 Like

Hey, thanks for the response. Although I believe it will work, when I tested it, there was a problem.

ServerScriptService.RoomHandler:25: attempt to perform arithmetic (div) on nil and number
if (GenString / 10) % 1 == 0 or GenString == 1 then
		GenString += 1
	end
	
	local StringLen = math.ceil(math.log10(GenString))

If the answer to this question is clear, I’ll just say that I haven’t explored every aspect of Lua.

(lets not talk about my math side either)

Change it to this real quick:

if GenString and ((GenString / 10) % 1 == 0 or GenString == 1) then
	GenString += 1
end

Your problem may be happening because GenString isn’t being set at all.

1 Like

ServerScriptService.RoomHandler:29: invalid argument #1 to 'log10' (number expected, got nil)

Ah- wait. I overlooked something.

You see, this doesn’t work because PlayerV is the Player. This is a Remote, and it has to pass the exact Player who called the Remote for security reasons.

1 Like

What secure technique would I use to fire this or get anything else we intend to do?

Script that fires the RE:

local Button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Button.MouseButton1Click:Connect(function()
	if script.Parent.Parent.UsernameBox.Text ~= nil then
		ReplicatedStorage:WaitForChild("BookRE"):FireServer(script.Parent.Parent.UsernameBox.Text, game.Players.LocalPlayer)
	end
end)

Also, not forgetting, sorry if I do waste your time :smile:

I appreciate you putting somewhat your time into this.

First things first, you don’t need to pass game.Players.LocalPlayer, that’s passed as the First argument with every call to :FireServer() (In other words, it’s “PlayerV” in the OnServerEvent connection.

I’m assuming, based on your client script that calls FireServer, that you’re trying to pass a Player’s Username to the Server. So in that case, you’d replace this code:

game.ReplicatedStorage.BookRE.OnServerEvent:Connect(function(PlayerV, CheckedIn)
	local Player = game.Players:FindFirstChild(PlayerV)

with

game.ReplicatedStorage.BookRE.OnServerEvent:Connect(function(Client, PlayerV)
	local Player = game.Players:FindFirstChild(PlayerV)

But if you’re actually just trying to give the person who pressed the button you’ve got set up, you’d just do this:

game.ReplicatedStorage.BookRE.OnServerEvent:Connect(function(Player, CheckedIn)

And remove the

local Player = game.Players:FindFirstChild(Player)

entirely.

I didn’t see anywhere that you actually used CheckedIn so I removed it as from the parameters. If you do have a use for it, you can add it back.

Lastly,

local Button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Button.MouseButton1Click:Connect(function()
	if script.Parent.Parent.UsernameBox.Text ~= "" then
		ReplicatedStorage:WaitForChild("BookRE"):FireServer(script.Parent.Parent.UsernameBox.Text)
	end
end)

We don’t need to pass game.Players.LocalPlayer so I removed it.
Try these fixes and let me know what happens.

1 Like

Woah! You actually saved me, thank you so much.

I appreciate you taking your time into this topic, and continue have a good rest of your day!

It did continue the function and did exactly as I did want it to do
image