English Can someone rate my script?

24 replies
Goto Page
To the start Previous 1 2 Next To the start
07.07.17 05:09:23 am
Up
baRD
User
Offline Off
Plz rate my script from 1-10 or reply to me what i need to improve?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
--=======================--
--==+ Player Shop v.1 +==--
--====+ by bardecon +====--
--=======================--

--+ Variables +--
c=string.char(169)

--+ Hooks +--
addhook("say","sayShop1")
addhook("menu","sayShop2")
addhook("buy","buy")

--+ Functions +--
function sayShop1(id,txt)
     if(txt=="!Shop")then
          menu(id,"Shop,Laser|$16000,Molotov|$1000")
     end
return 1
end

function sayShop2(id,title,button)
     if(title=="Shop")then
          if(button==1)then
               if(player(id,"money")>=16000)then
               parse("equip "..id.." laser")
               parse("setmoney "..id..""..player(id,"money")-16000)
               msg2(id,c.."255255000»You have succesfully bought a Laser!«")
          else
               msg2(id,c.."255000000»You dont have enought Money!«")
               end
          end
          
          if(button==2)and(player(id,"money")>=1000)then
               if(player(id,"money")>=1000)then
               parse("equip "..id.." molotov")
               parse("setmoney "..id..""..player(id,"money")-1000)
               msg2(id,c.."255255000»You have succesfully bought a Molotov!«")
          else
               msg2(id,c.."255000000»You dont have enought Money!«")
               end
          end
     end
end

function buy()
     return 1
end

This script is not yet done...
Quote:
I love lua scripting and do you have a problem with that?
07.07.17 06:21:44 am
Up
CallMeSteven
User
Offline Off
I rate it 8/10, but please upload your script in file archive
Mine is mine.
07.07.17 07:31:37 am
Up
Mora_rus
User
Offline Off
Dont upload it on file archive.
On question what you need to improve i tell you:
What's actually this lua planned to be? What do you want to see there yourself? What's your thoughts about to do with this lua after you done?
S.T.A.L.K.E.R. - The Lost Land dev. https://discord.gg/aABCkHQ
07.07.17 07:44:38 am
Up
baRD
User
Offline Off
Im planning to make a custom shop for my server cosmic ctf...
im still working on it
07.07.17 08:45:10 am
Up
Ortimh
User
Offline Off
What kind of rating do you want? Performance, beauty?

In terms of performance, well you don't use anything "that" heavy besides several function calls and creations, and conditions, so nothing to worry about performance, except one global variable that's used for nothing other than holding character 169 (you could use
\169
inside string dammit).

Contrawise for the beauty, you need to properly tab your scripts and add necessary whitespaces between words (not symbols). It would improve human reading for you, and for the coders who want to learn. Also remember that there's no convention you need to follow, like you need to use pascal case for global vars. Here's what I call a beauty.
Code:
function Person(name)
     local new = {}
     new.name = name
     
     return setmetatable(new, {
          __index = {
               call = function(self)
                    print('Hey, ' .. self.name .. '!')
               end
          }
     })
end

Code:
local person = Person 'Orlando Hutapea'

person.call()


Also there's no rating scripts with scale like 0-10. Just saying.
I'm awesome ... and I really cookies
07.07.17 09:47:37 am
Up
Bowlinghead
User
Offline Off
Let´s pretend you have a lot of other items in the shop. And you want to change the price of 5 items or sth. You would have to change it in line 17 (menu) and in line 27 (where you loose it) for each weapon (lot of work and everyone makes mistakes)

You could zip your offers into a table:
Code:
1
2
3
4
5
myWeapons = { 
     ["Laser"] = 16000,
     ["Molotov"] = 1000,
     -- todo
}


And the rest is all code with no real values whatsoever.
It should look like this:
Code:
1
2
3
4
if (player(id,"money") >= myWeapons[v]) then
     parse("equip "..id.." "..myWeapons[k])
     parse("setmoney "..id.." "..player(id,"money") - myWeapons[v])
end


This makes the script way to complex and long.
But once its done its very easy to edit weapons with pricing and stuff
Share time limited free games here
07.07.17 02:23:57 pm
Up
Fraizeraust
Moderator
Offline Off
user Ortimh has written:
except one global variable that's used for nothing other than holding character 169 (you could use
\169
inside string dammit).

What's the matter on using
string.char()
? I indeed understand he could've declared his variable as local and that he should move the variable inside
sayShop2
function since he's using it for a specific block scope but whether you use \169 or string.char() you get the same final result.
07.07.17 02:35:18 pm
Up
baRD
User
Offline Off
Hey bowling head what does this means?
Code:
1
2
3
4
if (player(id,"money") >= myWeapons[v]) then
     parse("equip "..id.." "..myWeapons[k])
     parse("setmoney "..id.." "..player(id,"money") - myWeapons[v])
end


I actualy need to know this because im actualy working in my custom player shop script
Spoiler >

Its just a menu...
Still working in the buttons...
edited 1×, last 07.07.17 02:45:40 pm
07.07.17 02:47:59 pm
Up
Ortimh
User
Offline Off
@user Fraizeraust: Yes, it would get the same result. But in terms of performance, there must be a difference between creating a variable and not creating a variable at all, right? Although it's unnoticable, it wouldn't be a good practice if you let yourself creating variables for unnecessary things (like for character 169 in this case) if there're other better ways. And... if you look closely,
string.char(169)
would do more work: accessing table; calling function; and creating a literal. While if you do the
\169
, it's already part of the string.
edited 1×, last 07.07.17 02:52:12 pm
I'm awesome ... and I really cookies
07.07.17 02:50:32 pm
Up
Fraizeraust
Moderator
Offline Off
@user baRD: Here's an explanation for you, piece by piece how this code works:

• 1st line: It'll create a conditional IF statement which checks if the player's money is greater than or equal the amount required to purchase a certain item. If the condition is deemed as true then it'll pass to the conditional body code (the further lines below);

• 2nd line: The regular
parse()
function to execute ordinary CS2D commands. What it does is to tell CS2D to equip you with the item you've purchased.

• 3rd line: Finally, it'll set the player's money based on purchase acquisition.

By the way,
[k]
stands for key which in our case is the item string you see in the
myWeapons
table while
[v]
stands for value which is the actual money value. Keep in mind that tables in Lua are somewhat associative arrays. You should take a look at Lua pil or other tutorials to see how tables actually work.

@user Ortimh: I see, you're right. But still, it will only apply to bigger scripts where performance is a critical step, for smaller scripts it doesn't really make sense.
07.07.17 02:57:23 pm
Up
Ortimh
User
Offline Off
@user Fraizeraust: But that doesn't make the excuse to let yourself into a bad habit like creating unnessary variables.
I'm awesome ... and I really cookies
07.07.17 03:29:12 pm
Up
baRD
User
Offline Off
So it equips both of the items?...
07.07.17 03:39:52 pm
Up
Avo
User
Offline Off
Creating a variable for the color character would make sense only if it could change in the future (editing all occurrences in a big script isn't what one wants to do).

Moreover, name "c" isn't a good name for a variable. A global variable with such name (in terms of length) is a bad habbit. Short names are good for a local-scope variables (loop interating ones, like "i" in for-loops). And what is important, it says nothing about purpose of this variable.
colorSign = string.char(169)
is more clear when you see it in the middle of someone's code.
07.07.17 03:51:17 pm
Up
baRD
User
Offline Off
umm i need one question too...
what does local vars do?

and i need help!!!
Spoiler >

its says
Code:
1
'<eof>' expected near 'end'
07.07.17 04:13:43 pm
Up
Bowlinghead
User
Offline Off
The error means you miss an end. Every for, if, etc. needs an end.
EDIT: See line 62. You start with an "elseif" which is wrong, you always start with an if.
Solution: Delete the end on line 61
(EDIT2: My edit is complete bullshit. You TABed wrong and I didnt read carefully)

Answer to your question which user Fraizeraust already answered:
Instead of equipping "laser" like you do, I equip whatever is in the table.
As I explained, I´d do this to easily edit the code later on.

Loops (for loops - you better read into it they help A LOT!!!) usually look like this:
Code:
1
2
3
4
5
6
for k,v in pairs(myWeapons) do
     -- now k and v are local variables
     -- k(ey) is like a counter, or in this case the name of the weapon
     -- v(alue) is the value inside, or in this case the price of the weapon (see my code above)
end
-- now, k and v do not exist anymore because they were local


Local variables only exist for the actual scope (or: they only exist until the 'end') like k and v above.

You are probably new to programming/lua so feel free to ignore my idea because you would have to rewrite almost everything.
Actually you asked where you can improve and I gave you an idea.

And btw, you can use elseif on line 25-51 to improve perfomance.
edited 1×, last 08.07.17 12:58:07 am
Share time limited free games here
07.07.17 04:20:39 pm
Up
Ortimh
User
Offline Off
@user Avo: I must say that creating the variable is useless after all because what other character that could change the color of text?

@user Bowlinghead Using elseif doesn't improve performance. It just uses different logical condition.

EDIT:
Sorry, misclicked your username, user Mora_rus.
edited 1×, last 07.07.17 05:28:24 pm
I'm awesome ... and I really cookies
07.07.17 04:35:00 pm
Up
baRD
User
Offline Off
i fixed that but this wont work!!!
Spoiler >

it wont show up the msg and wont make my money less...
07.07.17 05:18:16 pm
Up
Mora_rus
User
Offline Off
@user Ortimh: k nvm
@user baRD: not that, you need to return after code(before end)
Code:
1
2
3
4
5
6
7
function open_sayShop(id,txt)
     if(txt=="!Shop")or(txt=="!shop")or(txt=="!pshop")or(txt=="!pShop")or(txt=="!playerShop")or(txt=="!playershop")or(txt=="!PlayerShop")then
          menu(id,"Player Shop,Handguns,Shotguns,SubMachineguns,Rifles,Machineguns,Grenades,Gears")

          return 1
     end
end


dont use multiple hooks please
Code:
1
2
3
4
5
6
7
function helpCommand(id,txt)
     if(txt=="!help")or(txt=="!hlp")or(txt=="!Help")then
          msg2(id,"©255255000--+| Commands |+--")
          msg2(id,"©255255000!shop or !Shop --+| To Open Shop Menu... |+--")
     return 1
     end
end
edited 1×, last 07.07.17 05:50:18 pm
S.T.A.L.K.E.R. - The Lost Land dev. https://discord.gg/aABCkHQ
07.07.17 05:24:05 pm
Up
baRD
User
Offline Off
what do you mean about that?
To the start Previous 1 2 Next To the start