How would I get a returned value from an event

  1. What do you want to achieve? Keep it simple and clear!
    I want to get better at Roblox Scrpting

  2. What is the issue? Include screenshots / videos if possible!
    I do not know how to get a returned value from an event

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked in the API Refrence and couldn’t find anything of getting a returned value from an event

Thank you

I was talking about events like

baseplate.Touched:Connect(function()


end)

But RemoteFunctions are fine as well

1 Like

Ok, so let’s say you want to see if something gets added to a folder.

[ This same idea applies to Remote/Bindable Events/Functions! You can pass arguments in the same way, but I’m just doing it in a practical sense.]

First, you need to define said folder:

local folder = workspace:WaitForChild("FolderName")

Then, you can hook up the .ChildAdded() event, and at the same time get the returned argument(s):

local folder = workspace:WaitForChild("FolderName")

local connection --> i've defined this in case you want to call :Disconnect() on it!
connection = folder.ChildAdded:Connect(function(newChild) --> 'newChild' is the new instance.
    newChild.Name = "i changed it's name" --> you can reference this new instance
end)

You can view (most) instances, their events, and what they return over on the API Reference!

If I recall, you could just easily return the Value you want depending on how you’re wanting to use it (ObjectValues would be 1 example)

local function AddValue(PassedValue)
    local NewValue = PassedValue + math.random(1, 50) 
    return NewValue
end

workspace.NumberValue.Changed:Connect(function(CurrentValue)
    local CheckValue = AddValue(CurrentValue) --This would be referenced as our NewValue in our AddValue function (Or NumberValue + Random)
    print(CheckValue)

    if CheckValue == 50 then
        print("This value is equal to 50!")
    else
        print("This value is not equal to 50!")
    end
end)

oh

Then for example things like Touched, Changed, etc will have a argument returned.
If you want to like get who touched a Baseplate you can define a simple argument inside the function your connecting which will be the part that was touched.

local connection

connection = baseplate.Touched:Connect(function(part)
if not part.Parent:FindFirstChild('Humanoid') then
return
end

print(part.Parent.Name, 'has touched me!')
connection:Disconnect()
end)