Forum

> > CS2D > General > lua scripting failure
ForenübersichtCS2D-ÜbersichtGeneral-ÜbersichtEinloggen, um zu antworten

Englisch lua scripting failure

10 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt lua scripting failure

vrkiller
User Off Offline

Zitieren
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

alt Re: lua scripting failure

DC
Admin Off Offline

Zitieren
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'

alt Re: lua scripting failure

vrkiller
User Off Offline

Zitieren
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 ?

alt Re: lua scripting failure

Lee
Moderator Off Offline

Zitieren
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.

alt Re: lua scripting failure

vrkiller
User Off Offline

Zitieren
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

alt Re: lua scripting failure

Lee
Moderator Off Offline

Zitieren
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.

alt Re: lua scripting failure

DC
Admin Off Offline

Zitieren
leegao hat geschrieben
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!

alt Re: lua scripting failure

Lee
Moderator Off Offline

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

and do not use else if, use elseif

alt Re: lua scripting failure

vrkiller
User Off Offline

Zitieren
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>'
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antwortenGeneral-ÜbersichtCS2D-ÜbersichtForenübersicht