Remote Event printing "Unable to cast value to Object"

Hello, I have an issue with this script. I want to send a specific value from a table from the server to the client, but it prints “Unable to cast value to Object” for no reason. Also, I heard that you can only send objects to the clients, which is problematic. Here is the code.

-- From the Server
local tables = {}
--
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(plr, reason, detainedtime)
	table.insert(tables, plr)
	table.insert(tables, reason)
	table.insert(tables, detainedtime)
	print(tables)
	print(tables[1])
 -- Printing works perfectly.
	wait(2)
	remoteEvent:FireClient(tables)
end)
-- From the Client
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local plr = game.Players.LocalPlayer.UserId
local reason = "Go to heaven"
local detainedtime = 60

remoteEvent:FireServer(plr, reason, detainedtime)

remoteEvent.OnClientEvent:Connect(function(player, tables)
	print(tables[1]) --  Doesn't work there.
end)

Thanks in advance.

Remember that FireClient requires a player object as the first argument.

remoteEvent:FireClient(tables)

So it’s:

-- From the Server
local tables = {}
--
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(plr, reason, detainedtime)
	table.insert(tables, plr)
	table.insert(tables, reason)
	table.insert(tables, detainedtime)
	print(tables)
	print(tables[1])
 -- Printing works perfectly.
	wait(2)
	remoteEvent:FireClient(plr, tables)
end)

Another thing:

OnClientEvent does not pass the player, because it is already obvious who the player is (yourself, the client)

remoteEvent.OnClientEvent:Connect(function(player, tables)

So:

-- From the Client
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local plr = game.Players.LocalPlayer.UserId
local reason = "Go to heaven"
local detainedtime = 60

remoteEvent:FireServer(plr, reason, detainedtime)

remoteEvent.OnClientEvent:Connect(function(tables)
	print(tables[1]) --  Doesn't work there.
end)

Final and last thing:

FireServer does not require Player to be sent…

remoteEvent:FireServer(plr, reason, detainedtime)

So:

-- From the Client
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local plr = game.Players.LocalPlayer.UserId
local reason = "Go to heaven"
local detainedtime = 60

remoteEvent:FireServer(reason, detainedtime)

remoteEvent.OnClientEvent:Connect(function(tables)
	print(tables[1]) --  Doesn't work there.
end)
2 Likes