When to use remote and bindable functions/events

I’ve just started scripting recently, and I discovered things like BindableEvents and RemoteEvents etc.

I don’t really know when to use them though.

I’ve searched around, and it seems like you use it for scripts to start things in other scripts, but I still have a couple questions.

If a ClickDetector, or something, was in a block, and it was clicked, should I use a event to trigger another script, or just find it.

Example:

game.workspace.Part.ClickDetector.MouseClick:Connect(ExampleFunction)

Thank you!

1 Like

Events:
In addition to properties and functions, every object also has events which can be used to set up cause-and-effect systems. Events send out signals when specific things happen in a game, such as a player touching an object or a player connecting to the game. To fire an event is to have it send out such a signal.

Functions:
A RemoteFunction is used to create in-game APIs that both the client and the server can use to communicate with each other. Like BindableFunction , a RemoteFunction can be invoked (called) to do a certain action and return the results.

A BindableFunction is a Roblox object that allows you to give access to functions to external scripts. Functions put in BindableFunctions will not be replicated, therefore making it impossible to use these objects to pass functions between scripts. Functions are invoked through BindableFunction:Invoke , which calls BindableFunction.OnInvoke .

3 Likes

For my example, on the ClickDetector, could you explain which to use, and why? I get what they do now, but I’m not sure when to use them.

ClickDetectors allow Scripts and LocalScripts to receive pointer input on 3D objects through their MouseClick event. They work when parented to BasePart, Model or Folder objects. They detect basic mouse events: enter, leave, left click and right click. Touch input on TouchEnabled devices also fires click events. The default control scripts bind the ButtonR2 KeyCode to interact with ClickDetectors using ContextActionService:BindActivate, which can also be used to override this. When using gamepads, the center dot triggers MouseHoverEnter/MouseHoverLeave. The bound activation button fires MouseClick.

Alright, thanks. Would using a certain option decrease lag, or would they be similar?

If you put multiple scripts into Click Detectors, then it could cause some lag. There is a solution by only needing 1 script, using this method:
https://www.lua.org/pil/7.3.html

or you can use module scripts:

This channel is also a good learning source:

Rick Astley - Never Gonna Give You Up (Official Music Video) - YouTube

2 Likes

You would often use functions when you want yield or wait for a value to be returned.

For events, they don’t yield and they don’t return anything, they simply send a message

Hey, could you help me again?
I want to know, should I use a event, or a BoolValue for this?
I want to make something (a ViewportFrame, to be exact) disappear when the loading screen disappears. What should I use?

It is unnecessary to make an event, in this case. In the script that makes the loading screen disappear, just locate the ViewportFrame and make it disappear.

Oh, okay, thanks. My ViewportFrame is made using a script though, so what would I do in that case? Should I try locating it where it is created?

Use :WaitForChild(“ViewportFrameName”)

Okay. Would it matter if the thing is in PlayerGUI? Would I just do the same thing?

A bit confused here. My script creates a ViewportFrame that is parented to a ScreenGUI. When I playtest the game, it shows up in PlayerGui. Where should I find that? I didn’t really get what you said by

Yes. You are able to use PlayerGui.

1 Like

Would something similar to this work?

-- When the loading screen is disabled
local player = game.LocalPlayer --Would this be correct?
local playerUI = game.player.PlayerGui
local viewportframe = playerUI.ScreenGui:WaitForChild("ViewportFrame")

viewportframe.visible = false

game.Players.LocalPlayer doesn’t work on server script.
If the below is a server script then tell me:

-- When the loading screen is disabled
local player = game.Players.LocalPlayer --Would this be correct? Yes.
local playerUI = player:WaitForChild(“PlayerGui”)
local viewportframe = playerUI.ScreenGui:WaitForChild(“ViewportFrame”)

viewportframe.visible = false
1 Like

Yes, I made edits to mine. So I would just add the :WaitForChild()?

(Uhh… sorry for asking another question, but can you clarify on what :WaitForChild() does? I’m pretty sure it allows the script to wait for the thing to load or something, but I’m not sure)

Returns the child of the Instance with the given name. If the child does not exist, it will yield the current thread until it does.

1 Like

Sorry to bug you with even more, but I noticed this in your 1st post:

Aren’t you talking about functions inside code, not a RemoteFunction, or a BindableFunction?

1 Like