Jump to content

[LUA] LESSON #2 outputChatBox


Saml1er

Recommended Posts

Hello,

so since we learned how to create a resource and thanks to our friend firas for explaining us what is function and how it works. I've decided to continue lua lessons so today I'll teach you guys that what is " outputChatBox " and how it works. But first it's important to know that there are 2 types of script which are " Client side and server side " There's a rule in lua that you can't have client side function in server side and server side function in client side. But still there's a way to make it working we'll learn that in coming lessons right now we'll learn about " outputChatBox "

Task: So here's a simple task when we start the script it should say "Welcome to DDC Scripting" in yellow color (rgb = 255 , 255 , 255, 0, )

Solution:

outputChatBox ( " Welcome to DDC Scripting ", getRootElement(), 255, 255, 0, true ) 

so you see you made a simple script. Let's say welcome to scripting lvl 1, you learned how to make a simple script for outputChatBox. You can also have this in red color just change the rgb code ( here's a link for rgb codes )

red color

outputChatBox ( " Welcome to DDC Scripting ", getRootElement(), 255, 0, 0, true ) 

So now I'll explain how this works so when you add your text between " " you add the text and when you add getRootElement(), then you make the text visible for everyone and when you add 255,255,255 this is for the color and we are adding true ( if it is false then you can't add hex code like # FFFF00 between " " for example " #FFFF00Welcome #0000FFto #FFFF00DDC ".

so now I want to make a little change in here I want to output some text in chatbox when a player join the server so for that purpose you'll see the code a little different but don't get confused you can read firas topic here for more details.

function welcomeMessage ( ) -- welcomeMessage you can modify it with any name you want 
    outputChatBox ("Welcome to DDC server!", source, 255, 255, 0, true) -- you can modify the text between " " also the rgb code 
end -- the function ends here 
addEventHandler("onPlayerJoin", getRootElement(), welcomeMessage) -- adding an eventhandler so we can tell the script when to work 

So was that hard ? let's make something even good.

function welcomeMessage () -- welcomeMessage you can modify it with any name you want 
    local Dreddy = "good guy" 
    local Sam = " bad guy" 
    outputChatBox (Dreddy.." Welcome to DDC server! "..Sam, 255, 255, 0, true) 
end 

So this is how your meta.xml should look like

<meta> 
    <info author="Author" version="1.0" name="Script01" type="script"/> 
        <script src="script.lua" type="server"></script> 
</meta> 

So now I have a task for all of you reading this. Make a script saying " You all suck " in red color ( 255, 0, 0 ) onplayerjoin also if you add something like I did for example local Sam = "bad guy" then you might get some points ;D

NOTE: All these scripts are in server side, I'll add client side later.


Client side.

function welcomeMessage () -- welcomeMessage you can modify it with any name you want 
    local Dreddy = "good guy" 
    local Sam = " bad guy" 
    outputChatBox (Dreddy.." Welcome to DDC server! "..Sam, 255, 255, 0, true) 
end 
addEventHandler("onClientPlayerJoin", getRootElement(), welcomeMessage) -- so you see, we changed here "onPlayerJoin" to "onClientPlayerJoin" because it's client side. 

Now you must change this line in your meta.xml for client side.

<script src="script.lua" type="server"></script> 

to


Some info by Karevan from DDC:

Why are we using type"client" for this script?

It's because it's client side and you must tell meta.xml that the script is in client side or the script won't work so better change it.

The local "keyword" can be used or not, that is correct. But it's not meant to be used whenever you want to not "get confused". It has its uses. When you use local before the name of a variable, you are making that variable exist only WHITHIN that block of code and its childs, but not parents. So for example: if you have a local variable inside a function, that variable will not exist outside that function. But, if you declare a variable WITHOUT using local, that variable will then exist outside the function, and you will be able to use it on any other functions. local is used in a few more ways, but that's all you need to know for now.

Now you know that a variable is a keyword which stores a value virtually, you need to know that such value can either be a number, a string (text), or more complex things like tables or objects/elements. In the example, the values are strings. When you want to represent a string, you always have to use quotes around it. It can be either ' or ", but both need to be the same.

The actual difference between client and server, explained basically:

The scripts you run on the server, run on a machine which is common for all players, that machine synchronizes all players and shares their information. Server scripts run just once and on one machine.[/li]

The scripts you run on a client run on the PC of each player. The client "talks" with the server to know about the other players, where they are, what they do, etc... Client scripts run once on every player. This means you can have the same script running in different ways on different machines, with different players. But having the same code.[/li]

So, while things that happen in server happen at the same time for all connected clients (or players), things that happen on client happen in a different time for each client (or player).

Updated 21.02.2014

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...