PDA

View Full Version : need a lil help


Phobos
05-14-2009, 03:09 PM
Hello,
im a beginner with lua scripts.
as expected my first command did not gave the desired results.
could someone help me, whats wrong with this code?

The code executes.
but when i type that command ingame, it always shows the "weed" effect, even if i type /drug coke or /drug heroin or /drug

function BuyDrugs(PlayerID,Text)
local drugtype={}
if drugtype==nil then
outputChatBox(PlayerID,"Syntax: /drug <weed or coke or heroin>",255,0,0,255)

elseif isPlayerSpawned(PlayerID) == 0
then
outputChatBox(PlayerID,"Error: You must be spawned.",255,0,0,255)

elseif strval(drugtype)==strval("weed")
then
togglePlayerDrunk (PlayerID,1)
outputChatBox(PlayerID,"You consumed Weed, you are high!",255,0,0,255)

elseif strval(drugtype)==strval("coke")
then
setPlayerGravity(PlayerID,0.8)
outputChatBox(PlayerID,"You consumed Cocain, you are high!",255,0,0,255)

elseif strval(drugtype)==strval("heroin")
then
togglePlayerDrunk (PlayerID,1)
setPlayerGravity(PlayerID,0.8)
outputChatBox(PlayerID,"You consumed Heroin, you are high!",255,0,0,255)
end
end

addCommand("drug","BuyDrugs")

Boylett
05-16-2009, 02:35 AM
You defined drugtype as a blank table, and never assigned it to anything else. If you want to use "/drug" with a parameter, e.g. "/drug weed" then for addCommand you pust say how many parameters it has:

addCommand("drug","BuyDrugs",1)

Then you must use the value in the parameter of the callback, e.g.:

function BuyDrugs(PlayerID,drugtype)
if drugtype==nil then
outputChatBox(PlayerID,"Syntax: /drug <weed or coke or heroin>",255,0,0,255)

elseif isPlayerSpawned(PlayerID) == 0
then
outputChatBox(PlayerID,"Error: You must be spawned.",255,0,0,255)

elseif (drugtype)==("weed")
then
togglePlayerDrunk (PlayerID,1)
outputChatBox(PlayerID,"You consumed Weed, you are high!",255,0,0,255)

elseif (drugtype)==("coke")
then
setPlayerGravity(PlayerID,0.8)
outputChatBox(PlayerID,"You consumed Cocain, you are high!",255,0,0,255)

elseif (drugtype)==("heroin")
then
togglePlayerDrunk (PlayerID,1)
setPlayerGravity(PlayerID,0.8)
outputChatBox(PlayerID,"You consumed Heroin, you are high!",255,0,0,255)
end
end

;)

EDIT: And i just realised you're using strval to compare strings. Strval converts string to numbers, so that wouldn't work.

Phobos
05-16-2009, 08:24 AM
Thanks for that!
its working fine :)

i wish to add something else to that code:


.
.
.
elseif (drugtype)==("weed")
then
togglePlayerDrunk (PlayerID,1)
outputChatBox(PlayerID,"You consumed Weed, you are high!",255,0,0,255)
delay(60000) -- To stop the drunk effect after 60 seconds
togglePlayerDrunk (PlayerID,0)
.
.
.


but the rest of the scripts stop working until the 'delay(60000)' statement is executed.
Is there a alternative for that effect?

Mex
05-16-2009, 11:25 AM
Thanks for that!
but the rest of the scripts stop working until the 'delay(60000)' statement is executed.
Is there a alternative for that effect?

yep

you can use a timer like so:
createTimer("FunctionNameToCall", MSDelay, Repitions)
http://wiki.gtagaming.com/Timer_Functions/createTi mer

unfortunately you cant currently send parameters to functions with timers.
so theres 2 things you could do:

you could make a global table at run time, then when a player uses the drugs it could add their player ID and MS runtime of the server to the table, like so:
runtime:
playersUsingDrugs = {}
in the 'BuyDrugs' function:
createTimer("stopDrugEffect", 60000, 1)
playersUsingDrugs[PlayerID] = os.clock()


then when the 'stopDrugEffect' function is called, it will loop around that global table and check which players have been high for 60 secs or longer. Which can be done like so:
for i,v in pairs(playersUsingDrugs) do
local CurrentMS = os.clock()
local a = CurrentMS - v
if a >= 60000 then
-- player with ID 'i' has been high for 60 secs or more
playersUsingDrugs[i] = nil
togglePlayerDrunk(i, 0)
end
end

if you used this method you'd also need to add this to onPlayerDisconnect() and onPlayerDeath()
playersUsingDrugs[PlayerID] = nil

or you could use the 'VLETimer' and 'VLETimerCall' functions in VLE, like so:

VLETimer("stopDrugEffect" .. PlayerID, 1, 60000, "stopDrugEffect", PlayerID)
function stopDrugEffect(PlayerID)
playersUsingDrugs[PlayerID] = nil
togglePlayerDrunk(PlayerID, 0)
end