Hello, I’m trying to Invoke a client, get a response and proceed with the script. But the script runs before getting a response. Have I done something wrong or have I missed something?
–Script
local text = game.ReplicatedStorage.Stages.TextRequest:InvokeClient(plr) -- event
local textinstance = game:GetService("TextService"):FilterStringAsync(text,plr.UserId,Enum.TextFilterContext.PublicChat) -- Runs this before getting a response.
local textresult = textinstance:GetNonChatStringForUserAsync(plr.UserId)
itemsPart.Text.TextLabel.Text = text
– Local Script
local plr = game.Players.LocalPlayer
local function request()
plr.PlayerGui.TextRequest.Enabled = true
plr.PlayerGui.TextRequest.Frame.Submit.MouseButton1Down:Connect(function()
local text = plr.PlayerGui.TextRequest.Frame.EnterText.Text
plr.PlayerGui.TextRequest.Enabled = false
return text
end)
end
game.ReplicatedStorage.Stages.TextRequest.OnClientInvoke = request
Well, the main issue here is that the server only calls upon the function once (before the client loads) and the client sets a function which returns some text.
Additionally, within your request function, you connect an event to then return a value to request. This is not possible.
Instead of calling the function from the server and setting it on the client, create the function on the server and invoke it from the client. Something sorta like this:
Script
local text = game.ReplicatedStorage.Stages.TextRequest.OnClientInvoke = function(player, text)
local textinstance = game:GetService("TextService"):FilterStringAsync(text,plr.UserId,Enum.TextFilterContext.PublicChat) -- Runs this before getting a response.
local textresult = textinstance:GetNonChatStringForUserAsync(plr.UserId)
itemsPart.Text.TextLabel.Text = text
end
Localscript
local plr = game.Players.LocalPlayer
plr.PlayerGui.TextRequest.Enabled = true
plr.PlayerGui.TextRequest.Frame.Submit.MouseButton1Down:Connect(function()
local text = plr.PlayerGui.TextRequest.Frame.EnterText.Text
plr.PlayerGui.TextRequest.Enabled = false
game.ReplicatedStorage.Stages.TextRequest:InvokeServer(text)
end)
Well, it seems you’ve figured it out. But for future reference, no. You only need one remotefunction.
The idea was that the function is set on the server and then the client would invoke the function with the text it wants set. Then, the server would perform the operation on it and then return the new text.
Take for example, what I wrote in my original comment. There is only one function needed. If you need the text back, simply add a return text at the end of the function.