Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make a remote event return a value?

  • Local Script

local Keypad = workspace.Keypad.Keys local Keys = Keypad:GetChildren() local Code = Keypad.Parent.Code local Working = Keypad.Parent.Working local Event = game:GetService("ReplicatedStorage"):WaitForChild("KeypadClicked") local one = Keypad:FindFirstChild("1") local two = Keypad:FindFirstChild("2") local three = Keypad:FindFirstChild("3") local four = Keypad:FindFirstChild("5") local five = Keypad:FindFirstChild("4") local six = Keypad:FindFirstChild("6") local seven = Keypad:FindFirstChild("7") local eight = Keypad:FindFirstChild("8") local nine = Keypad:FindFirstChild("9") one.ClickDetector.MouseClick:connect(function() Event:FireServer(1) end) two.ClickDetector.MouseClick:connect(function() Event:FireServer(2) end)

three.ClickDetector.MouseClick:connect(function() Event:FireServer(3) end)

four.ClickDetector.MouseClick:connect(function() Event:FireServer(4) end)

five.ClickDetector.MouseClick:connect(function() Event:FireServer(5) end)

six.ClickDetector.MouseClick:connect(function() Event:FireServer(6) end)

seven.ClickDetector.MouseClick:connect(function() Event:FireServer(7) end) eight.ClickDetector.MouseClick:connect(function() Event:FireServer(8) end) nine.ClickDetector.MouseClick:connect(function() Event:FireServer(9)local Keypad = workspace.Keypad.Keys local Keys = Keypad:GetChildren() local Code = Keypad.Parent.Code local Working = Keypad.Parent.Working local Event = game:GetService("ReplicatedStorage"):WaitForChild("KeypadClicked") local one = Keypad:FindFirstChild("1") local two = Keypad:FindFirstChild("2") local three = Keypad:FindFirstChild("3") local four = Keypad:FindFirstChild("5") local five = Keypad:FindFirstChild("4") local six = Keypad:FindFirstChild("6") local seven = Keypad:FindFirstChild("7") local eight = Keypad:FindFirstChild("8") local nine = Keypad:FindFirstChild("9") one.ClickDetector.MouseClick:connect(function() Event:FireServer(1) end) two.ClickDetector.MouseClick:connect(function() Event:FireServer(2) end)

three.ClickDetector.MouseClick:connect(function() Event:FireServer(3) end)

four.ClickDetector.MouseClick:connect(function() Event:FireServer(4) end)

five.ClickDetector.MouseClick:connect(function() Event:FireServer(5) end)

six.ClickDetector.MouseClick:connect(function() Event:FireServer(6) end)

seven.ClickDetector.MouseClick:connect(function() Event:FireServer(7) end) eight.ClickDetector.MouseClick:connect(function() Event:FireServer(8) end) nine.ClickDetector.MouseClick:connect(function() Event:FireServer(9) end)


-Server Script


local Event = game:GetService("ReplicatedStorage").KeypadClicked local InputCode = script.Parent.InputCode local Code = script.Parent.Code local Working = script.Parent.Working Event.OnServerEvent:Connect(function(num) InputCode.Value = InputCode.Value..num if string.len(InputCode) < 3 then InputCode.Value = "" end if InputCode.Value == Code.Value then Working = true end end)


-Output


16:46:10.594 - Workspace.Keypad.Script:6: attempt to concatenate local 'num' (a userdata value)


2 answers

First, try printing num

Coincidentally, this happened to me some time ago when I was working on a daily reward system.

The problem is, that when you fire the server, the first value is the player it came from. Now, since you only have one argument, that argument, num is the player. Before num, you need to add something like player

Here's what your code should look like:

local Event = game:GetService("ReplicatedStorage").KeypadClicked
local InputCode = script.Parent.InputCode
local Code = script.Parent.Code
local Working = script.Parent.Working

Event.OnServerEvent:Connect(function(player, num)
	InputCode.Value = InputCode.Value..num

	if string.len(InputCode) < 3 then
		InputCode.Value = ""
	end

	if InputCode.Value == Code.Value then
		Working = true
	end
end)

Hope I helped!

well you can't. that's why you use remote functions

remote events are event based, which means you can only wait for the other side(client/server) to trigger an event by sending data;

on the other hand,remote functions work much like your own functions. which means, when you do remoteFunction:InvokeClient() it will actually halt the script until the callback function registered by the other side returns

for instance:

client:

local RF = game:GetService("ReplicatedStorage").RemoteFunction

function CountDown(countingTime)
	if countingTime < 1 then
		return :"error"
	end
	for i=1,countingTime,1 do
		print(i)
	end

	return "success"
end

RF.OnClientInvoke = CountDown

server:

local RF = game:GetService("ReplicatedStorage").RemoteFunction
local player --imaginary player--

print(RF:InvokeClient(player,20)) --//success

when we do: RF:InvokeClient(player,20) the script will halt until the CountDown function from the client returns

This discussion is preserved as it was. New answers, comments, and votes are closed.