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

How do you make a remote event return a value?

Asked by
Mpire_z -2
4 years ago
  • 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

Log in to vote
0
Answered by
U_srname 152
4 years ago

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!

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question