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

Help on understanding a script?

Asked by 8 years ago

Uh... I've watched a video from @ScriptGuider on how to make a Custom Chat. I really don't understand very well a few parts of what he wrote, so I would appreciate an explanation of some bits of code that I'll indicate. As I want to learn on how to FilteringEnabled, I understand the structure in general on how it works, but there is parts that I don't understand, so I hope I get some more understanding of this after this post!

ServerChat > ServerScriptService

--\\ Services
local RepStorage = game:GetService'ReplicatedStorage'

--\\ Remotes
local ChatEvent = RepStorage:WaitForChild'ChatRemote'

--\\ Remote event
ChatEvent.OnServerEvent:connect(function(Client, Request, Value)
    Request = Request:lower()
    if Request == "chat" then
        ChatEvent:FireAllClients("chat", Value, Client.Name)
    end
end)

ClientChat > StarterGui


--\\ Services local RepStorage = game:GetService'ReplicatedStorage' local Players = game:GetService'Players' local StarterGui = game:GetService'StarterGui' --\\ Remotes local ChatEvent = RepStorage:WaitForChild'ChatRemote' --\\ Client stuff local Client = Players.LocalPlayer local Mouse = Client:GetMouse() --\\ GUIs local Chatbar = script.Parent local ChatFrame = Chatbar.Parent --\\ Chat messages local Chats = {} -- Why is this Chat table empty? --\\ Push messages local function PushMessages(Size) -- How does this argument identify himself as the Size of the GUI? for i,v in pairs(Chats) do v.Position = v.Position - UDim2.new(0,0,0,Size) if v.Position.Y.Offset < -ChatFrame.AbsoluteSize.Y then Chats[i] = nil v:Destroy() end end --\\ Create message local function CreateMessage(Message, Speaker) -- What are the argument's utility here? local Label = Instance.new("TextLabel") Label.BackgroundTransparency = 1 Label.TextXAlignment = "Left" Label.TextColor3 = Color3.new(1,1,1) Label.Font = "ArialBold" Label.FontSize = "Size18" Label.Size = UDim2.new(1, 0, 0, 25) Label.Position = UDim2.new(0, 0, 1, -25) Label.Text = tostring(Speaker) .." : " ..Message PushMessages(Label.Size.Y.Offset) Chats[#Chats+1] = Label -- What does this line do? Label.Parent = ChatFrame end Mouse.KeyDown:connect(function(Key) if Key:byte() == 47 then Chatbar:CaptureFocus() -- CaptureFocus() Is a function of the Mouse? end end) Chatbar.FocusLost:connect(function(Enter) -- Does the Enter argument identify himself as the Key:byte() that's equal to Enter? if Enter then if Chatbar.Text == '' then return end ChatEvent:FireServer("chat", Chatbar.Text) Chatbar.Text = '' end end) ChatEvent.OnClientEvent:connect(function(Request, Message, Speaker) -- What are this arguments for? Request = Request:lower() if Request == "chat" then CreateMessage(Message, Speaker) end end) StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

My questions about this are:

What does OnServerEvent function do?

Is it a communication between the Server and the Client?

What does OnClientEvent function do ?

Is it a communication between the Client and the Server?

I'll include commentary questionsinside the script.

0
I want to make this clear. We are not a substitute for your brain. Everything we learned, you can learn the same way. User#6546 35 — 8y
1
I know, I've checked the Wiki links of each thing, But I just want a quick resume. For me to clarify what is it. I'm not asking for a 30 pages explanation because that's located in the Wiki. I just want a small answer on each question so I understand it better. iDarkGames 483 — 8y
0
And I know I can learn the same way, but that doesn't mean that I can't receive a bit of a push on understanding stuff that is not very clear for me in the Wiki. iDarkGames 483 — 8y
1
OnServerEvent is the event fired when a client is using the FireEvent function on a RemoteEvent. The server takes action with the event taking place and runs the function you define in the connection line. Basically the same way around for the OnClientEvent just for server to client communication. M39a9am3R 3210 — 8y
View all comments (3 more)
0
Thanks M39a9am3R, that's better than the wiki. iDarkGames 483 — 8y
1
These type of understanding type questions are fine, ignore elunate Perci1 4988 — 8y
0
I'll do. I just want a better understanding of it. I'm not telling you to explain me all the script, just bits that I don't understand. iDarkGames 483 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

RPCs

You don't need to know specifically what RPCs are, but just that in this case I'm going to be referring to RemoteEvents and RemoteFunctions as RPC handles or similar. If you do want to know, the RPC is the request which they put across the network. As far as you're concerned at this level, we'll assume they're mostly magic.


OnServerEvent

The OnServerEvent member Event of the RemoteEvent class is fired when a LocalScript uses the :FireServer method. The first argument of OnServerEvent is the Player who the LocalScript belongs to, and the other arguments are whatever was put into FireServer

RemoteEvent:FireServer(arg1, arg2, ...)
--> OnServerEvent(Player, arg1, arg2, ...)

OnClientEvent

The OnClientEvent member Event is almost identical to the OnServerEvent event, but instead it's from the server to the client. The parameters of OnClientEvent are the extra arguments put into FireClient or FireAllClients

RemoteEvent:FireClient(Player, arg1, arg2, ...) -- To a specific Player
RemoteEvent:FireAllClients(arg1, arg2, ...) -- To all Players.
--> OnClientEvent(arg1, arg2, ...)
Ad

Answer this question