Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I wait until a remote event is finished?

Asked by 5 years ago

I don't know how stupid this question is but I really need an answer... I am trying to make it so that when a remote event is fired, it waits until the fired event is fully finished to continue. The script below types out the message, but I need it to wait until the message loop is fully finished to continue..

replicatedStorage.RE.Story.StoryMsg:FireClient(plr,"msg1")
wait(5) -- right now it has to be perfectly timed to continue 
replicatedStorage.RE.Story.StoryMsg:FireClient(plr,"msg2")

This is the script that handles the event:

function type_out(msg)
local sentence = msg
local label = script.Parent
wait()
local len = string.len(sentence) 
    wait()
    local n = 0 
repeat
    wait(0.05)
    n = n + 1
    label.Text = string.sub(sentence,1,n).."#" -- The '#' is simply for the effect
    script.Parent.Parent.Parent.Parent.Typing:Play() -- a typing sound
until n >= len
     label.Text = sentence
end
game.ReplicatedStorage.RE.Story.StoryMsg.OnClientEvent:Connect(type_out)

I tried remote functions, but it broke my script and I have never used them before.

0
i have a terrible idea if u wanna hear it DinozCreates 1070 — 5y
0
...sure .-. TheGreenSuperman 85 — 5y
0
at the end of the first remote event, send one back to the client from the server, that would then send the second message. its horrible i know but its also amazing. DinozCreates 1070 — 5y

1 answer

Log in to vote
5
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Took me a moment to realize that you don't need the server to do any text manipulation at all since it will be a waste a time for the server. You should note that anything meaningless like this should be kept on the client.

A typewriter effect can easily be achieved using for-loop.

local text = "This is a sentence."

--using a text label from a GUI
local textLabel = someDefinedGui.TextLabel

for i = 1, #text do
    textLabel.Text = string.sub(text, 1, i)

    wait(1)
end

The above adds a new character from the string to textLabel.Text at a 1 second interval.

Wrapping this in a function should be really simple, but all this should be kept on the local script.


RemoteFunctions

So another useful tool when working with client-server communication is the RemoteFunction. This is used to allow whatever you're communicating to return information back (be it server or the client). Something relevant to your question is that a RemoteFunction will wait or yield until what is requested is completed.

Say we wanted the server to add two numbers together because the player gave some values:

-- this is a server script in ServerScriptService

-- We have a RemoteFunction in ReplicatedStorage
local RS = game:GetService("ReplicatedStorage")
local addNumbers = RS:WaitForChild("RemoteFunction")

function onAddNumbers(player, a, b)

    print("waiting for 5 seconds")
    wait(5)

    return a + b
end

addNumbers.OnServerInvoke = onAddNumbers

In our function onAddNumbers() we have three arguments: The player parameter is required by the OnServerInvoke event since it is the client communicating. It is then followed by parameters a and b (Values passed by client).

Notice that in the function we also have a wait(5)? Well I'm using this to test the yielding of a RemoteFunction.

After waiting, the function will return the sum of a and b to the client.

-- local script in StaterPlayerScripts

local RS = game:GetService("ReplicatedStorage")
local addNumbers = RS:WaitForChild("RemoteFunction")

--Invoke or 'request' the server by passing the values the player 'gave'
local calculated = addNumbers:InvokeServer(2, 5)
print(calculated)

The variable calculated stores whatever is return by the server after invoking and yield is completed.

We then obviously print it.


If you find the way how binding OnServerInvoke is done with a = you could see why here

The article may be confusing so let me know if you need more of an explanation on that.


To conclude:

Many use cases for this is wanting the client request the server for some player data. Like from some player DataStore which the server will return a result.

Note that you should be careful when requesting the server for information from the client. You should never trust the client when it's asking for information from the server, so you will need to have the server-side circumvent that. There are a few articles on game protection for that which you could find yourself.

Let me know if I missed anything since this type of question doesn't need to go this far into server-client communication.

Ad

Answer this question