RemoteFunction not returning properly

I’m trying to make a notification RemoteFunction which signals the client to create a notification using the StarterGui SetCore function. It all works, but it’s not waiting until anything has been returned. I’ve tried adding a repeat wait but that doesn’t seem to help.

Client code:

local response = script.Response

function callback(text)
	response.Value = text
end

local bindableFunction = Instance.new("BindableFunction")
bindableFunction.OnInvoke = callback

game.ReplicatedStorage.Remotes.Notification.OnClientInvoke = function(title,text,icon,duration,button1)
	game.StarterGui:SetCore("SendNotification",{Title = title,Text = text,Icon = icon,Duration = duration,Callback = bindableFunction,Button1 = button1})
	if button1 == nil then
		return
	else
		response.Changed:Connect(function(v)
			if v ~= "" then
				response.Value = ""
				print(v)
				return v
			end
		end)
	end
end

Server code:

local response = game.ReplicatedStorage.Remotes.Notification:InvokeClient(v,"Bank",player.Name.." has started a bank heist","",5,"Teleport to bank")
print(response)
repeat
    print("test")
    wait(.05)
until response
if response == "Teleport to bank" then
    print("teleport")
end

This is my first time posting so please give me feedback! :grimacing:

First, congrats on your first topic post!

From my tests, the function does wait for a response. The reason it does not seem like it is because as soon as the server invokes the client, the client sends data right back so quickly that you do not notice. If you add a wait on the client right here:

game.ReplicatedStorage.Remotes.Notification.OnClientInvoke = function(title,text,icon,duration,button1)
   game.StarterGui:SetCore("SendNotification",{Title = title,Text = text,Icon = icon,Duration = duration,Callback = bindableFunction,Button1 = button1})

   wait(3) -- I ADDED THE WAIT HERE.

   if button1 == nil then
   	return
   else
   	response.Changed:Connect(function(v)
   		if v ~= "" then
   			response.Value = ""
   			print(v)
   			return v
   		end
   	end)
   end
end

You will notice the server is still waiting for the player’s response before going back and executing the repeat function on the server.

It didn’t make any difference.

With that being said, could you supply the first portion of the server script?

I noticed the variables like “v”, and “player” are not established within the code you given. I just assumed “v” was the player so I used the player for that, and player.Name.

1 Like

Just to let you know, waiting for the client to fire back to the server isn’t exactly the greatest idea. Although that is a feature, RemoteFunctions should mainly be used if you’re calling the server from the client and then returning something from the server. I understand that you are attempting to make a notification system for the client, but a bit more context would be helpful in this situation, for example, what is the client returning to the server?

I’m going to assume that you are attempting to have it teleport the player to the bank when they click the notification button. If this is the case, it would most likely be better to use a RemoteEvent to fire to the server, and then go from there.

Some reworked client-side code would look something like this:

local responseEvent = [[path to RemoteEvent here]] 

function callback(text)
	responseEvent:FireServer() -- You can add additional parameters here
end

game.ReplicatedStorage.Remotes.Notification.OnClientInvoke = function(title,text,icon,duration,button1)
	game.StarterGui:SetCore("SendNotification",{Title = title,Text = text,Icon = icon,Duration = duration,Callback = bindableFunction,Button1 = button1})
end

Then you could update the server-side to fit your needs.

If you need any more help, let me know!

3 Likes