How do I wait until this function has finished running?

local script

local function shove(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		
		game.ReplicatedStorage.movesetRemoteEvents.shoveRemoteEvent:FireServer()
		print("completed")
		
	end
end

server script

game.ReplicatedStorage.movesetRemoteEvents.shoveRemoteEvent.OnServerEvent:Connect(function(plr)
	
	print(plr.Name .. " has shoved")
	wait(5)
	
end)

What I want to happen is for it to wait until the function is finished, and then print completed, meaning what I should see in the output is
“(player) has shoved”
5 seconds later
“completed”

but instead what I see is
“completed”
“(player) has shoved”
so i can tell that its absolutely not doing that- how do I make it wait until the function has ended?

Use a RemoteFunction instead of a RemoteEvent and it will be as you describe. RemoteFunctions can return things so the code always waits for the RemoteFunction to finish.

Use a remote function instead, it will yield the script until it’s completed. Also make sure to wrap the server code in a pcall because it can easily error

is this a general tip, or specifically when using a remote function

is there any functional difference between using a remote function here and a remote event besides the yielding i should be aware of, or does it strictly solve my problem and do nothing else

Definitely not a general tip. Pcall will keep your code running if there is an error, but you shouldn’t use it unless the errors are out of your control. In this case, if the RemoteFunction is never returned by the LocalScript due to networking issues, script deletion, or other issues and causes an error, you can detect that with a pcall and then keep your Script running.

Nothing you need to really worry about in this scenario. Really it just acts more like a function where it waits to receive a response (if any). Of course you set it up differently if you’re unfamiliar:

game.ReplicatedStorage.movesetRemoteEvents.shoveRemoteFunction:InvokeServer()
game.ReplicatedStorage.movesetRemoteEvents.shoveRemoteFunction.OnServerInvoke = function(player)
    -- do stuff
end
2 Likes

Just for remote functions because of errors, for example if the player who invoked the remote function leaves mid way through, the script will error

The difference is that remote functions are able to return values, and remote events are not (plus the yielding part).

Remote events are either Server → Client or Client → Server
Remote functions are either Server → Client → Server or Client → Server → Client

1 Like

just to make sure, i dont need to have the remote function return anything for it to work/yield, right?

EDIT: also, just making sure this is like an actually good idea, and i shouldnt be doing something totally different, my idea is to use this for context action service so i can keep the debounce client sided and simple

No, if you don’t return anything it will just give nil

Because cheaters can invoke remote functions whenever they want, you should record the last time the user tried to shove somebody and make sure that the debounce has been accounted for on the server. Your strategy would work for regular players, but you need to make sure you’ve got sanity checks for the cheaters aswell.

but it will still yield, right

in that case, should i just do what i did before (use a remote event, have a bool value instance on the player’s client for debounce, send it through to the remote event as a parameter, and handle debounce that way)?