Remote Function Problem

I don’t know how to explain the problem but I will try to.

I am invoking the client using remote function.

--server script
print(chat)  -- output Hi

--local script
RemoteFunction.OnClientInvoke = function(1,chat)
print(chat) --output nil

I have no idea why this is hapening.

Always look in the Output window to see if you get errors.

You’re getting an error with that code, because identifiers (e.g. variable or parameter names) can’t start with numbers.

You also don’t have an end to end the code block that you open with the function keyword.

To fix your syntax:

RemoteFunction.OnClientInvoke = function(number,chat)
    print(number, chat) --output 1, Hi

You are receiving the parameters incorrectly, also you should never use invoke client, because hackers can leave the server yielding, unless you handle them properly, and quite frankly there is never a good case in which this is even necessary.

-- do this
RemoteFunction.OnClientInvoke = function(_,chat)

I don’t have any numbers as parameter I just put that their as an example and that is not all of my code, of course, I have an end

What do you mean? (30 charrrrr)

Are you sure chat is not being modified before invoking the client?

if CanSpeak == true then

Chat = chats[math.random(1,#chats)]


Chat = nil


Only this.

InvokeClient is a yielding function, so on the server when it is called it will yield the current thread until a result is returned. On the client an exploiter can easily override the script where you are processing the remote function and make it yield the server indefinitely.

I don’t think that has anything to do with my problem.

It does not but you should never be using invoke client.

print() the value of Chat after the conditional statement and check if it’s correct.

I have a print() after this

and before this

and after this

On the devhub page roblox says in practice there is no real reason for the server to invoke the client: RemoteFunction | Documentation - Roblox Creator Hub

What should I use then? (30 char)

I agree with cjj, even though it’s not your main problem. It shouldn’t be used carelessly. Use a RemoteEvent instead. It does not yield the thread.

What are trying to do exactly?

It also doesn’t seem like the server or the client is expecting a result from one another. A RemoteEvent is the exact same thing, but no yielding is occurring. It’s better if you’re not expecting a response from the server or the client.

Only use a RemoteFunction to invoke the server from the client when a value is needed before resuming code execution on the client. In the rare case of invoking a client from the server, it should be done in a thread or an event. So it doesn’t yield the main thread if it errors.

I have a UI where the player chats with a npc. I send what the npc is going to say with the remote function and when the chat is finished I will return something to continue the code and I also have a code if the player leaves it destroys the npc.

Why do you need to send this over from the server? Everything can be done on the client, and on the client when they are finished talking to the npc, fire a remote event to the server.

because I have a String Value in server storage and the chat is a child of that String value the npc choses one of the String Values from the server and then checks it’s child and chooses what to say.