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)
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
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:
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.
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
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.
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)
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:
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.