"Unable to cast value to Object" error when firing client in a ModuleScript

I am firing a ModuleScript in ServerScriptService that would call a function, from a server script that is also in ServerScriptService. This part seems to work fine, however there is a problem with the second part.

Then, the function in the ModuleScript that I just called fires the client and the client (the client is a LocalScript in ReplicatedFirst) should call a function in another ModuleScript. This might sound a bit complicated, so for anyone confused as to why I want it to work this way, one ModuleScript stores all server sided functions that I will have to use many times, and the second ModuleScript stores all client sided functions that I will have to use many times. In this case, a function from one ModuleScript needs to trigger a function from another one, thus one of the modules has to call the other one.

The code does not want to fire the client from the ModuleScript and errors “Unable to cast value to Object”
(I did not include anything unnecessary like path variables and other functions that don’t play a role here at all)

local ServerModule = {}

ServerModule.Functions = {
	["Func1"] = function(Player) -- This function gets called by the ServerScript and works
-- Some code that works
			RemoteEvent:FireClient("ChangeGui") -- CODE STOPS WORKING, UNABLE TO CAST VALUE TO OBJECT
		end
	end
}

return ServerModule

LocalScript that recieves the :FireClient and calls another ModuleScript:

if (not game:IsLoaded()) then
	game.Loaded:Wait()
end

--Recieves the argument from the ModuleScript and based on the Arg given triggers the specific function stored in another ModuleScript's table.
RemoteEvent.OnClientEvent:Connect(function(Arg)
	if OnClientModule.Functions and OnClientModule.Functions[Arg] then		
		OnClientModule.Functions[Arg]()
	end
end)

ModuleScript called by the LocalScript:

local ClientModule = {}

ClientModule.Functions = {
	["ChangeGui"] = function()
		Sign.Visible = true
		Sign.Text = "Team won!"
	end
}

return ClientModule

The first argument for :FireClient() is a Player object so it knows which player you are trying to send the message to!

Try:
RemoteEvent:FireClient(Player,"ChangeGui")

Or if you are trying to fire that event for all players use:
RemoteEvent:FireAllClients("ChangeGui")

Hope that helps!

2 Likes

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