How do I communicate a local value to a server based object?

Hello. I am making a player-controlled plane but I’m having trouble pointing it in the right direction. Mouse.hit is a Cframe value, and AlignOrientation can be made to point towards CFrames. However, Mouse.hit is a local value, and I don’t know how I can get a local value through to a server-based AlignOrientation. I would like it if someone could point me to the right instance or service or whatever is needed for this process.

So, the only way you can really do it is the following:

  1. Insert Remote Function in Replicated Storage (Call it like GetMouseHit)
  2. Insert local script in StarterPlayerScripts
  3. Put the following:
--// Services
local Players = game:GetService("Players") 
local RS = game:GetService("ReplicatedStorage")

--// Variables
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local GetMouseHitEvent = RS.GetMouseHit

--// Functions
local function GetMouseHit()
    return Mouse.Hit

--// Connections
GetMouseHitEvent.OnServerInvoke = GetMouseHit

Now in your other script you just gotta InvokeClient on that remote function. If there is something you don’t get in this script, feel free to let me know!


I got a bit of an issue here. With OnServerInvoke there, it returned an error stating that it could only be called on the server. I replaced it with OnClientInvoke and the error was gone, but the plane refuses to point. Could there be a reason in that script for this, or should I look through my other code?

Speaking of which, here’s the entire code. The plane is the startercharacter rather than a vehicle. If this has anything that conflicts with that, it might be why.

Pilot = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
h = game.ReplicatedStorage.GetMouseHit:InvokeClient(Pilot)
while true do
	script.Parent.CFrame = h

I am very inexperienced and the little experience I do have is very rusty, so any help would be greatly appreciated.

I am very sorry, it was supposed to be OnClientInvoke, try what does h print?

I wouldn’t recommend using InvokeClient as the server will yield until it gets a response from the client, which may result in an infinite yield. RemoteEvents are preferred for server-to-client.

RemoteEvents can’t return data/a value. Remote Event’s won’t work in this situation since we need to return the Mouse.Hit value.

Instead, you can use a RemoteEvent system to replicate how a RemoteFunction would operate. The benefit of this is allowing customization over how the server handles waiting for a response. This could include a timeout period.

1 Like

Yeah, you could i guess. If your afraid of yields you can spawn a new thread aswell though, and even handle the invoke in a pcall

While that’s true, spawning a thread or using pcall still won’t avoid the risk of infinite yields. I would recommend creating a module to handle a custom method (as I mentioned earlier) so that you can reuse it with ease in future projects.

1 Like

h doesn’t print at all for some reason.

The plane is quite literally the player character, so infinite yield seems to me like a moot point. If the player disconnects, their plane simply disappears.

If nothing is returned, it will infinitely yield. Thankfully it will break if the player disconnects, but it’s better to use your own system. Or actually a better method I just remembered is to just create a handler for the server’s end. Just handle the invoke in a new thread and loop until either a response or after a period of time. If there is no returned value, then break the thread.

I feel so stupid right now. I needed to put the scripts in startercharacterscripts.

Alright, so I’ve worked on the issue and the scripts actually run instead of disappearing, but now the script is throwing errors at me. The localscript is free of errors but the main script says “Argument 1 is missing or nil” when I try to find h. Do you know why this might be?

Did you pass in the Player as a parameter?

I did not. I accidentally made it getplayerfromcharacter from the torso.

UPDATE: turns out instead of that, I made it getplayerfromcharacter from the workspace. Instead of adding parents I should have removed them.

And do you have any extra listed parameters in your local script?

I did not add any extra parameters. Upon fixing the getplayerfromcharacter issue I am no longer seeing errors, and my plane does initially respond to the mouse (although it becomes unresponsive after a few seconds). I will do a little more tweaking of the actual AlignOrientation before anything else.

1 Like