FireClient can only be called from the server - formerly known as "cast to object" error

As in my recent article, I found out that I needed to provide a variable. Now, I decided to convert the script into a localscript in an icon, located in the frame. I may not understand FireClient() as intended, but I’m seeking assistance into a solution for this. Here’s my code:

-- In the "Icon" imagebutton
local rep = game.ReplicatedStorage.SetIcon
rep.OnClientEvent:Connect(function(plr, txt)
	print("User has typed "..txt.." in the textbox!")
end)
-- In the "Submit" button
local rep = game.ReplicatedStorage.SetIcon
local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	rep:FireClient(plr, script.Parent.Parent.TextBox.Text)
end)

image

What’s the concurrent issue that creates that specific error? If possible to elaborate on, I’d appreciate it.

What is the error? Could you include a screenshot of the output or just write the error message?

It’s provided in the title, “FireClient can only be called from the server”

You’re trying to FireClient, change it to FireServer, and remove plr

change
rep:FireClient(plr, script.Parent.Parent.TextBox.Text)

to

rep:FireServer(script.Parent.Parent.TextBox.Text)

2 Likes

FireClient() may only be called in a server script, and you’re calling it from a LocalScript. Replace FireClient() with FireServer() and get rid of the player argument in it.

3 Likes

rep:FireServer(script.Parent.Parent.TextBox.Text)

1 Like

RemoteEvents are used for server to client communication, and vice versa. Try using a BindableEvent instead.

Not the purpose here, this is from client to server.

1 Like

RemoteFunctions are Client to Server, however, you’re right about RemoteEvents. BindableEvents are typically for server → server or client → client

1 Like

The thing is, when I change it into “FireClient”, it won’t pop up as the other script has “OnServerEvent”, producing an error of “OnServerEvent can only be used on the server”

1 Like

Considering these are both local scripts you can combine them into one script and put it under icon and remove the remote event entirely

-- in the "Icon" image button
local function SetIcon(txt)
print("User has typed "..txt.." in the textbox!")
end


script.Parent.Submit.Activated:Connect(function()
SetIcon(script.Parent.TextBox.Text)
end)
3 Likes

Didn’t think of that at all, thank you for the advice. I should consider that in the future, also thank you to all that supported, I do appreciate it.

2 Likes