How do I return a value from a remote event?

Hi! I am wondering how I can return a value from a remote event, like after firing it get the returned value. Say I want to transfer data from client to the server I fire a remote event from the server and I want to get data back from the client, how would I do that? Thank you - BMWLux :smiley:

2 Likes

You can’t, which is why RemoteFunctions exist.

4 Likes

So, these can work from the server to the client. Like I can fire it from the server and get back data from client?

1 Like

actually my bad do what @Downrest said I forgot that you can’t get the result from a remote event

1 Like

Here’s an example:

-- server

local RS = game:GetService("ReplicatedStorage")

local event = RS.Events.RemoteFunction

event.OnServerInvoke = function(player: Player)
	-- do stuff here
	
	return "hi there"
end
-- local

local RS = game:GetService("ReplicatedStorage")

local event = RS.Events.RemoteFunction

warn(event:InvokeServer()) -- this prints out 'hi there'

Do note that InvokeServer() yields (pauses the script and waits for something to be returned).

8 Likes

local RS = game:GetService("ReplicatedStorage")

local event = RS.GiveBackSoundLoudness

event.OnClientInvoke = function(player: Player, sound)
	return sound.PlaybackLoudness
end

So can I put a sound paramenter if i invoke the client and put in the player param and the sound

Like how can I run a second parameter through it?

function(player: Player, sound, yourParameter ) just use a comma and add another value

1 Like
local PBL = game.ReplicatedStorage.GiveBackSoundLoudness:InvokeClient(game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())], ch)

Ok this is my fire script.


local RS = game:GetService("ReplicatedStorage")

local event = RS.GiveBackSoundLoudness

event.OnClientInvoke = function(Player: player, sound, thing)
	print(thing)
	print(sound)
	return sound.PlaybackLoudness
end

This is my return script. Any ideas why its printing nil?

:arrow_double_up: But yeah no errors or anything, any ideas why it wont work?

But yeah no errors or anything, any ideas why it wont work?

1 Like

Try printing out ‘ch’, check if it’s nil.

If it isn’t, try printing out all variables inside ‘OnClientInvoke’ (e.g. player, sound, thing).

I have, ch isn’t nil. Any all of them print nil
image

1 Like

Could you tell me what ‘ch’ is?

1 Like

My exact script:

local OriginalSize = script.Parent.Handle.Mesh.Scale
local ts = game:GetService("TweenService")
print("PLAYING");
script.Parent.Handle.ChildAdded:Connect(function(ch)
	if ch:IsA("Sound") then
		ch.Changed:Connect(function()
			print("Changed");
			print(ch.Name)
			local PBL = game.ReplicatedStorage.GiveBackSoundLoudness:InvokeClient(game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())], ch)
			print(PBL)
			local Multiplier = PBL + 0.4
			print(ch.PlaybackLoudness)
			print(Multiplier)
			local Tween = ts:Create(script.Parent.Handle.Mesh, TweenInfo.new(0.05), {Scale = Vector3.new(OriginalSize.X * Multiplier, OriginalSize.Y * Multiplier, OriginalSize.Z * Multiplier)})
			Tween:Play()
			wait(0.05)
		end)
		script.Parent.Handle.Mesh.Scale = OriginalSize
	end
end)

It also prints fine. Prints the exact name

1 Like

Maybe try sending in ‘ch.Name’ and find the exact sound in the client itself?

Alright I guess I’ll try that. Maybe I go into sound service and scroll through a possible sounds and find it.