Hello, I have this script which fires a remote function when a player sits:
local seat = script.Parent
local main = script.Parent.Parent.Main
local other = script.Parent.LocalScript
local model = script.Parent.Parent
local func = game.ReplicatedStorage.TransferEvents.Plane
local clone = other:Clone()
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant ~= nil then
local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
local pg = plr.PlayerGui
print("Cloned")
clone.Parent = pg
clone.Disabled = false
func:InvokeClient(plr)
print("Func returned")
else
clone.Parent = seat
clone.Disabled = true
end
end)
And this localscript which runs when the remote function is fired.
local func = game.ReplicatedStorage.TransferEvents.Plane
local useri = game:GetService("UserInputService")
local running = true
func.OnClientInvoke = function(plr)
useri.InputBegan:Connect(function(imput)
if imput.KeyCode == Enum.KeyCode.E then
print("Turned on")
end
end)
end
Ok so “cloned” prints when the player sits and the script is cloned, and “func returned” should print when the player is seated and presses E. But fr some reason it prints without the player pressing E, and according to the tutorial about this I watched, the remote function should not return to the other script until this script is finished(which means that the function should return when the player presses e). But for some reason it returns instantly when the player sits.
Thanks for reading.
This isn’t really how you use a remote function. Remote functions serve for 2 way communication, meaning that they send information and then receive information. Your local script is not returning any information to the server script, therefore you should use a remote event.
The reason it’s printing “Fun returned” right away is because the script is simply moving along each line. You never returned anything via. :InvokeClient, and there are no yields in your script-- there is simply nothing that would make the script stop running and wait for you to press E in order to say “func returned.”
Instead, what I would do if I were you is create a local script which detects when you press E. If you are sitting when you press E (which you can do via. local script), a remote event is fired and the Server receives it. Not sure why you need anything done on the server, but I’ll assume that you do indeed need it since that seems to be what you’re asking for.
The local script? The server script, sure: you can do RemoteEvent:OnServerEvent:Wait() (the remote event will be the one which is fired when you press E)
But why would you need to do this?
When the player seats, the localscript gets cloned to the player and, there, it should wait until the player presses E to turn on the plane in the serverscript.
I’m confused on what that meant. You don’t need to do any sort of “waiting” for this kind of thing. Simply try what I mentioned:
Instead, what I would do if I were you is create a local script which detects when you press E. If you are sitting when you press E (which you can do via. local script), a remote event is fired and the Server receives it. Not sure why you need anything done on the server, but I’ll assume that you do indeed need it since that seems to be what you’re asking for.
The server can handle the event with
RemoteEvent:OnServerEvent:Connect(function()
--Turn on plane here
end)
None of that stuff will run until the remote event is fired-- you won’t have to implement any sort of “wait.”
Sort of, you could make a while loop which breaks once the person presses E, but once again, why would you want to do this? You don’t seem to need any sort of remote function to do what you’re trying to do-- I told you how you can do this with a remote event.