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.
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.
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, ...)
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, ...)