I have a RemoteFunction that sends stuff from the server to the client, but the client doesn’t immediately return stuff to the server (it’s not supposed to). Due to this, it doesn’t get me the results I want.
How would I make the server wait until the client sends something back before doing anything? I can provide scripts if needed.
What is your use case? RemoteFunctions are intended to receive data from the server or from the client, just use a RemoteEvent.
Using a RemoteEvent you could simply yield using a repeat loop until the client fires the event with whatever data you need.
1 Like
Yeah. I’m using a RemoteFunction because I’m expecting data back.
Use a RemoteEvent, it’s probably better in your case.
local dataRecieved, dataSent = false, {}
event.OnServerEvent:Connect(function(plr, data)
dataRecieved, dataSent = true, data
end)
repeat wait() until dataRecieved
-- code will continue when the client fires the event
1 Like
I dont know…
I already wrote a lot of code around that RemoteFunction. It’d be a pain to rewrite it again.
falsefuul
(blackzeus)
April 8, 2021, 10:05pm
7
local data = Event:InvokeClient(player) -- data is whatever is returned.
print(data)
Is this what you’re trying to do?
The docs says that the server already waits.
I know, but it doesn’t. It just executes the code even though nothing has been returned yet, which is strange.
falsefuul
(blackzeus)
April 8, 2021, 10:30pm
10
That is strange, could you show us the code?
Ok
--Server script
local EventFolder = game.ReplicatedStorage.HPRCRemotes
local ClickDetector = script.Parent.ClickDetector
local Crank = game.Workspace.HPRC.Crank
local Display = game.Workspace.HPRC.Display.SurfaceGui.TextLabel
local Occupied = false
EventFolder.PlayerLeft.OnServerEvent:Connect(function()
Occupied = false
end)
ClickDetector.MouseClick:Connect(function(plr)
if Occupied == false then
EventFolder.KeypadClick:FireClient(plr)
Occupied = true
end
end)
EventFolder.NameInput.OnServerEvent:Connect(function(plr, plrName, playerFound)
if playerFound == true then
Display.Text = "SUCCESS! PLAYER FOUND."
wait(1)
local isLoading = true
local dots = 1
spawn(function()
while isLoading == true do
Display.Text = "WAIT"..string.rep(".", dots)
if dots < 3 then
dots = dots + 1
else
dots = 1
end
wait(0.8)
end
end)
local RequestedPlayer = game.Players:FindFirstChild(plrName)
local bruh = EventFolder.GetToCranking:InvokeClient(RequestedPlayer)
if bruh == true then -- problems start arising here
print("yes!")
elseif bruh == false then
print("no!")
else
print("Help") -- it would print this, meaning it's returning neither true or false
end
else
Display.Text = "ERROR!"
wait(0.2)
Display.Visible = false
wait(0.2)
Display.Visible = true
wait(0.2)
Display.Visible = false
wait(0.2)
Display.Visible = true
wait(0.2)
Display.Visible = false
wait(0.2)
Display.Visible = true
wait(0.2)
Display.Text = "PLAYER NOT FOUND."
wait(1)
Display.Text = "ENTER NAME."
end
end)
--Local script
local EventFolder = game.ReplicatedStorage.HPRCRemotes
local Frame = script.Parent.UsernameInput
local Request = script.Parent.Request
local timerRunning = false
local HurryUpNoob = coroutine.create(function()
timer = 30
while wait(1) do
if timerRunning == true then
if timer < 1 then
timerRunning = false
EventFolder.UrTooSlow:FireServer()
end
timer = timer - 1
print(timer)
end
end
end)
coroutine.resume(HurryUpNoob)
EventFolder.GetToCranking.OnClientInvoke = function()
Request.Visible = true
timerRunning = true
Request.Accept.MouseButton1Click:Connect(function(player)
Request.Visible = false
timerRunning = false
timer = 30
return true
end)
Request.Deny.MouseButton1Click:Connect(function()
Request.Visible = false
timerRunning = false
timer = 30
return false
end)
end
EventFolder.KeypadClick.OnClientEvent:Connect(function(plrName)
Frame.Visible = true
end)
They’re unfinished so some of the code might seem off but really all I want is to fix what I mentioned in my original post.
falsefuul
(blackzeus)
April 8, 2021, 10:42pm
12
I believe the problem is here, these mousebutton1clicks are just connecting and not waiting meaning its returning instantly. This should work:
EventFolder.GetToCranking.OnClientInvoke = function()
Request.Visible = true
timerRunning = true
local chosenOption = nil
Request.Accept.MouseButton1Click:Connect(function(player)
Request.Visible = false
timerRunning = false
timer = 30
chosenOption = true
end)
Request.Deny.MouseButton1Click:Connect(function()
Request.Visible = false
timerRunning = false
timer = 30
chosenOption = false
end)
repeat RunService.Heartbeat:Wait() until chosenOption ~= nil
return chosenOption
end
2 Likes
RemoteFunctions do not yield if the return is not within in the “original” scope. You will have to do something like this
local response
if x then
response = “yes”
end
repeat wait() until response
return response
1 Like