Error "Unable to cast value to Object" while using a RemoteFunction

What do you want to achieve?
I am trying to send a function to the client through a RemoteFunction, though I am getting the error “Unable to cast value to Object”

Here is the Server-Side script:

local Functions = ReplicatedStorage:WaitForChild("Functions")
local stopwatchFunction = Functions:WaitForChild("StopwatchFunction")

local minutes = ReplicatedStorage.Time.Minutes
local seconds = ReplicatedStorage.Time.Seconds
local timeElapsed = 0

function Stopwatch()
	local timeElapsed = 0
	while true do
		timeElapsed = timeElapsed + 1
		local minutesValue = math.floor(timeElapsed / 60)
		local secondsValue = math.floor(timeElapsed % 60)
		local timeString = string.format("%02d:%02d", minutesValue, secondsValue)
		stopwatchFunction:InvokeClient(timeString)
		task.wait(1)
	end
end

Stopwatch()

And here is the Client-Side script:

local functions = ReplicatedStorage:WaitForChild("Functions")
local stopwatchFunction = functions:WaitForChild("StopwatchFunction")

stopwatchFunction.OnClientInvoke = function(timeString)
	print(timeString)
end

For remote functions the first parameter passed must be a player.

Also I believe the function you are using would benefit more from a remote event. Remote functions are used when the server needs values only the client has. Remote events just send data to the other side.

Try replacing the remote function with a remote event and doing:

function Stopwatch()
	local timeElapsed = 0
	while true do
		timeElapsed = timeElapsed + 1
		local minutesValue = math.floor(timeElapsed / 60)
		local secondsValue = math.floor(timeElapsed % 60)
		local timeString = string.format("%02d:%02d", minutesValue, secondsValue)
		stopwatchFunction:FireAllClients(timeString)
		task.wait(1)
	end
end

then for the client:

local functions = ReplicatedStorage:WaitForChild("Functions")
local stopwatchFunction = functions:WaitForChild("StopwatchFunction")

stopwatchFunction.OnClientEvent:Connect(function(timeString)
	print(timeString)
end)
1 Like

Thank you so much! I assumed that a remote function would be better to store more info, seems I was wrong lol :sweat_smile:

1 Like

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