I am currently working on a system that assigns a specific room to a player when he calls up the menu. To check if the room is already in use by another player, I have created a table in a server script that stores the status of each room and returns a free room number to the client when called.
My problem is that I always get an error message because the returned value is zero.
I have tried debugging my code to check if there is an error. However, everything works except the part with “return i” (returns the number of the room). I have printed “i” to check if the value is nil, but it is not. I’ve also tried searching for similar problems, but I haven’t found anything that matches my problem.
Here is the part of the code:
Local Script:
-- Camera:
local room = HandleRoom:InvokeServer("LogIn")
print(room) -- prints nil
camera.CameraSubject = workspace.Menu:FindFirstChild("SpawnCage"..room)
print("success")
Server Script:
-- Services:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Events:
local HandleRoom = ReplicatedStorage.Events:WaitForChild("HandleRoom")
-- Instances:
local MenuFolder = workspace.Menu
-- Data:
local Rooms = {
["1"] = nil,
["2"] = nil,
["3"] = nil,
["4"] = nil,
["5"] = nil,
["6"] = nil,
["7"] = nil,
["8"] = nil,
["9"] = nil,
["91"] = nil,
}
local function AssignRoom(player)
print("1")
for i, v in pairs(Rooms) do
if v == nil then
v = player
return i -- should return the number of the room
end
end
end
local function Unassign(player)
for i, v in pairs(Rooms) do
if v.Name == player.Name then
v = nil
return true
end
end
end
local function CheckInput(player, input)
print(player, input)
if input == "LogIn" then
AssignRoom(player)
elseif input == "LogOut" then
Unassign(player)
end
end
HandleRoom.OnServerInvoke = CheckInput
Thank you for trying to help me!