RemoteFunction returns nil to client (prints value on server though)

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!

1 Like

It could be because you haven’t returned the room object back to the client at the end of the CheckInput function. Hope this helps!

Something like this:

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)
	local room
	if input == "LogIn" then
		room = AssignRoom(player)
	elseif input == "LogOut" then
		room = Unassign(player)
	end
    return room -- return the result from the function it runs
end
2 Likes

Check if the loop in the AssignRooms function runs at all. It might be, because you’ve set every Room key to nil , that you in reality don’t have any values because setting the a key to nil will remove the value. Try using another value, maybe -1, to indicate a room as being unassigned and checking for that instead.

2 Likes

You returned the value in AssignRoom() but did not return anything in the check input script which is what the remote function is connected to.
Do

if input == "LogIn" then
    return AssignRoom()

instead

2 Likes

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