Events: ¿difference?

I saw some scripts that are for example:

function onTouch(part)
--an example:
player.Leaderstats.Cash.Value =
player.Leaderstats.Cash.Value + 50

But some scripts include stuff like events. What’s the difference using them and not? I know they do a function but why they use it?

What are you trying to ask? It’s somewhat unclear, but I’ll try to answer what I think you’re asking.

A function does not necessarily have anything to do with events. Events need to be connected to a function, but functions are used beyond events, too.

1 Like

Events are an excellent way to make your game run more efficiently *
what people mostly use are Remote Events
when a remote event is fired from the client, another script, ideally in ServerScriptService can run like for example ,

local script in a gui button :

local event = game.ReplicatedStorage.Event

script.Parent.MouseButton1Click:Connect(function()

event:FireServer()--client has fired the event

end)

then a serverscript in ServerScriptService

local event = game.ReplicatedStorage.Event
Event.OnServerEvent:Connect(function(player)--when this event is fired..
	local tool = game.ServerStorage.tool
	tool:Clone().Parent=player.Backpack--we clone it to his backpack
end)

*when opposed to running several local scripts for an action

**you will have to insert a remote event in replicated storage first, that’s where they are usually kept.Here you can store things both serversided and clientsided.

1 Like

RemoteEvents have nothing to do with efficiency. It’s just Roblox’s way of communicating between client and server.

1 Like

actually they do make your game run more efficiently for example, when opposed to having run a local script each time an event is fired for separate tools as in my specific example.

And yes, that is how the server can communicate with the client and vice versa

over here in my older post you can see how using events can be efficient, for that purpose an alternative would be to use multiple server and local scripts, but that would cost you memory and affect latency also it’s a terrible idea

Events are also used because the client can’t access ServerStorage so you’ll have to use server scripts or remote events to access it.
Exploiters can’t access it either, when the event is fired a script can be run upon it being fired, when that happens the server can “communicate” and perform an action on the client.

This concept may be a little obscure since some developers aren’t knowledgeable about this datatype.

An event, which is a common term for the datatype RBXScriptSignal, refers to a special kind of Roblox object. The functions you attach via the Connect method are called listeners. These allow you to run certain actions when something happens in your game.

There are two ways to connect a listener to an event. These are done through the signal’s Connect method, something you should be very familiar with working with.

-- A
local function onTouched(hit)
    print(hit)
end

Part.Touched:Connect(onTouched)

-- B
Part.Touched:Connect(function (hit)
    print(hit)
end)

There are only two real differences between either of these methods and what you choose to use will depend on the context and necessities of your code.

The first example is if you’re looking for something readable and reusable. By defining a function to a variable, you can reuse this function in various ways. An example of where reusability may come handy is when you need to account for existing and new players in a data handler.

local Players = game:GetService("Players")

local function onPlayerAdded(player)
    -- Do something to player
end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

The second example comes in handy if you’re looking for a quick, accessible way to connect a listener to a signal when you have a function only relevant to that connection and it doesn’t need to be used elsewhere. This is a common way to write out functions and it is perfectly fine to do.

Signals are fired internally, so you as the developer are only able to connect to them with your listener functions. There is, however, an exception if you create a custom signal class with ModuleScripts or use BindableEvents, which allow you to make your own events to both connect to and fire.

Some signals will fire with some data values called arguments. Your listener functions will receive these as parameters. Have you ever written a function where you put something in the brackets? These are parameters. Some events will pass data through which your listener catches and works with. For example, the Touched event for a Part will fire a touched part to the signal and your listener function will receive which other part touched the current one.

-- Your listener
Part.Touched:Connect(function (Hit)

-- The signal internally
Part.Touched:Fire(touchedPart)

You can also, as mentioned earlier, achieve this behaviour using BindableEvents. You, as the developer, are able to fire off these signals with some data values. Think of this as indirectly calling your function with a couple of values. Your listener functions will receive this data and then run themselves with the data as usable variables.

This should set you good for events. To wrap things up, let’s review the information. Functions (actually listeners) and events (actually signals) are not one in the same, but they work together to achieve game functionality. You connect a function to an event and that function listens for the event to fire. This allows you to run actions when certain things in your game happen. Events for instances are fired internally by the engine and you can create your own using BindableEvents.

3 Likes

Solved by @XxELECTROFUSIONxX and @colbert2677.

1 Like