Forum

> > CS2D > General > lua scripting failure
Forums overviewCS2D overviewGeneral overviewLog in to reply

English lua scripting failure

10 replies
To the start Previous 1 Next To the start

old lua scripting failure

vrkiller
User Off Offline

Quote
this code is supoosed to give a respond of some "msg" if you typed "!sounds" in the chat but it dosent, how can that be?

1
2
3
4
5
6
7
8
9
10
function player_say(id,txt) 

	-- My Server Info
	if (txt=="!sounds") then
		msg("type: "s1" - Scream sound")
                             msg("type: "s2" - Die sound")	
	end


end

old Re: lua scripting failure

DC
Admin Off Offline

Quote
1. you didn't hook the function! cs2d will NEVER call it when you don't hook it.
1
addhook("say","player_say")

2. you are using quotes in a string which is defined with quotes. don't do that.
either put slashes (\) in front of the quotes which you want to have in your text or use single quotes to define the string:

"type: \"s1\" - Scream sound"
'type: "s1" - Scream sound'

old Re: lua scripting failure

vrkiller
User Off Offline

Quote
ok my code is now this:

1
2
3
4
5
6
7
8
9
addhook("say","player_say")

function player_say(id,txt) 
                
	if (txt=="!sounds") then
		msg("type: 's1' - Scream sound")
                             msg("type: 's2' - Die sound")	
	end
end

But it still dosent work, and i don't know how to do? Ho to do dc ?

old Re: lua scripting failure

Lee
Moderator Off Offline

Quote
if you use the raw and unformated txt from the event "say", the variable txt will have a trailing space, so whenever you type in "!sounds", it is passed in as "!sounds "

Try using if (txt == "!sounds ") then instead and see if that will work.

old Re: lua scripting failure

vrkiller
User Off Offline

Quote
i have made the code working but a problem again.... my code got 3 if


if(txt=="!usp")
{ ..... }
if(txt=="!glock")
{ ..... }
if(txt=="!deagle")
{ ..... }

when i enter "!deagle" it runs the usp code so i use

else if(txt=="!glock")

instead but it won't accept the "else" how can i fix that:

fully code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
addhook("say","player_say") 
function player_say(id,txt) 
    
     if(txt=="!usp") then
          parse("equip "..id.." 1") 
          parse("say you just got a !usp") 
         end       


    
    else if(txt=="!glock") then
          parse("equip "..id.." 2") 
          parse("say you just got a !usp")
         end else        

 
    
     else if(txt=="!deagle") then
         parse("equip "..id.." deagle") 
         parse("say you just got a !usp")
     end

end

old Re: lua scripting failure

Lee
Moderator Off Offline

Quote
the lua if control uses this syntax:

[code]if true then

elseif statement then

else

end[code]

Do not put an end in there until the whole condition is finished. Lua treats the whole if statement, not just the individual statements, as a block, so you can't terminate the block with trailing else.

old Re: lua scripting failure

DC
Admin Off Offline

Quote
leegao has written
if you use the raw and unformated txt from the event "say", the variable txt will have a trailing space, so whenever you type in "!sounds", it is passed in as "!sounds "

Try using if (txt == "!sounds ") then instead and see if that will work.


I just re-testet that and I do not have a space at the end of txt. Otherwise the sayfunctions.lua sample wouldn't work at all!

old Re: lua scripting failure

Lee
Moderator Off Offline

Quote
don't add an end after the all of the else statements have finished running

and do not use else if, use elseif

old Re: lua scripting failure

vrkiller
User Off Offline

Quote
my code is now this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
addhook("say","player_say") 
function player_say(id,txt) 
    
          if(txt=="!usp") then 
          parse("equip "..id.." 1") 
          parse("say you just got a !usp") 
     


    
          elseif(txt=="!glock") then 
          parse("equip "..id.." 2") 
          parse("say you just got a !usp")
         

 
    
         elseif(txt=="!deagle") then 
         parse("equip "..id.." deagle") 
         parse("say you just got a !usp")

end

but it shows a failure in console 'end' expected (to close 'function' at line 2) near '<eof>'

old Re: lua scripting failure

Lee
Moderator Off Offline

Quote
you forgot to close the function with an end, just add an end after the first end.
To the start Previous 1 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview