Jump to content

Lopexsw 2nd scripting attempt!


lopexsw

Recommended Posts

Hello everyone, It's Lopexsw again here!

So I have thought of a new script I want to make with your (The Pro scripters) help! My aim is to make a script that when a player gets into a certain vehicle for example 

Journey 508 Journey.png

 they have the option to do /cookmeth and then if they have the required ingredients in their inventory, they can start cooking meth! But if for example they don't have a gas mask (Which is possible to get in the server I play, but I doubt this script will be there ever.) they're health would go a bit down while they do it!

So as I see the steps are something along the lines of this:

 

  • When a player gets into a vehicle, check what vehicle that is.
  • If it's a Journey (id 508) then display message in chat that they have a option to cook meth.
  • *If it's not a journey don't show anything*
  • When the player writes /cookmeth check they'r inventory for certain items.
  • If they have the certain items, then freeze them for 3 minutes.
  • *If not, display message you can't do it*
  • At the same time display a countdown of the 3 minutes saying "3 minutes of cooking remaining" or something like that that goes down.
  • After 3 minutes, unfreeze the player.
  • Take the items required and give the meth to the player!

So 1 problem that I thought of, is this client side or a server side script? I think it's server sided.

 

As always, any help is hugely appreciated, and I will try to make something myself too, not just get a result from you guys! #1 priority is learning! #2 is a done well made script for someone's server! I think this will be a very intresting script!

Link to comment

First, use onVehicleEnter handler and bind it to a function that does the checks like getElementModel (check if the vehicle id == 508)

If yes, he can try to cook so do something like this

canCook =[]

canCook[player] = true

Then, addCommandHandler that triggers the cooking if player can cook only

If yes, check the inventory of the player.

I assume you have an inventory system, if not,

inventory = {}
addCommandHandler('getStuff', function (player)
 inventory[player] = {}
 inventory[player]['Gas Can'] = 2
 inventory[player]['Mask'] = 1

 outputChatBox('You got some stuff', player, 255,255,255)
)

  

Ok now you can check if a player has an item like this

if inventory[player]['Gas Mask'] >= 2 and inventory[player]['Mask'] >= 1 then
  -- can cook
  -- triggerClientEvent todisplay a timer and on screen gui
end

  

 

Link to comment
function giveoptions( thePlayer, seat, jacked)
	if (getElementModel (source) == 508) then
		outputChatBox( "Do /cookmeth to start cooking methamphetamine!", getRootElement(), 10,255,0, false)
		function cook (thePlayer, command)
			if inventory[player]['Gas Mask'] >= 1 and inventory[player]['Hidrogen Poroxide'] >= 1 and inventory[player]['Toxic soap'] >= 1 then
				outputChatBox("Now wait while the methamphetamine is beeing cooked", getRootElement(), 10,255,0,false)
				setElementFrozen(thePlayer, true)
				setTimer(setElementFrozen,180000, 1, false)
				--Start timer on clients screen
				--After, give the item
				outputChatBox("You have succesfully made some methamphetamine!", getRootElement(), 10,255,0,false)
				else outputChatBox("You don't have the required ingredients to cook methamphetamine!", 10,255,0,false)
			end
		end
	else nil
	end
end
addEventHandler ("onVehicleEnter", getRootElement(), giveoptions)
addCommandHandler("cookmeth", cook)

Okay, so this is what I've got so far.. I do not have any inventory, nor client side timer yet!

So the idea here is that, first it check the cars model if its = to 508, then it tells you the option of cooking meth, otherwise it doesn't do anything. Then if you do /cookmeth then it will trigger the function "Cook", then if a player has that random crap in theyr inv, they get a msg that it's cooking ad get frozen for 3 mins, then when the timer ends they should recieve a item (Not yet made that part) and after that, get a msg that they have made some meth, otherwise it says you don't have the ingredients.

Problems I faced that I would like some help with:

  1. I didn't get quite how the "addCommandHandler" works, since I know to enter the name without the / as first argument, to create a cmd in-game, but the other one i didn't quite understand from the wiki!
  2. How do I check if a play does a certain cmd? I undestood it as if someone does cookmeth, it will trigger my function "cook", thus starting the if etc.
  3. If I do a timer via "setTimer" does that mean, that ONLY AFTER the timer has run out, the next line of code will go? For ex where I do the 3 min timer, that only after the 3 minutes, it will continue, thus giving the person the end product?

Okay so as always any help is appreciated and remember the #1 goal here is for me to learn how to do this! It doesn't matter for me to get this on a server or something!

Link to comment

I don't think function cook will ever be executed, since it's undefined in the scope of the addCommandHandler, cook should be next to the handler.
 

function cook(...)
  -- do stuff
end
addCommandHandler("cookmeth", cook)

but then, this would allow anyone to use the command, so do it like this:

local playersInCookingTruck = {}
function giveoptions( thePlayer, seat, jacked)
  if (getElementModel (source) == 508) then
    playersInCookingTruck[thePlayer] = true
    -- rest of the function ...
  end
end
addEventHandler('onVehicleEnter', root, giveoptions)

then you check if player in cooking truck, in function cook

function cook(player)
  if not playersInCookingTruck[player] then return end -- stop, dont do the code below
  -- check for inventory
  setElementFrozen(player, true)
  -- ....
end

For the timer, no, it wont wait until the timer is done to execute the code below it, if you want it to wait, then

setTimer(function()
  -- after 18 seconds, do all this
  -- start client timer
  outputChatBox('You've got your meth', player)
  -- give him some meth
end, 18000, 1)

 

Link to comment
48 minutes ago, lopexsw said:

function giveoptions( thePlayer, seat, jacked)
	if (getElementModel (source) == 508) then
		outputChatBox( "Do /cookmeth to start cooking methamphetamine!", getRootElement(), 10,255,0, false)
		function cook (thePlayer, command)
			if inventory[player]['Gas Mask'] >= 1 and inventory[player]['Hidrogen Poroxide'] >= 1 and inventory[player]['Toxic soap'] >= 1 then
				outputChatBox("Now wait while the methamphetamine is beeing cooked", getRootElement(), 10,255,0,false)
				setElementFrozen(thePlayer, true)
				setTimer(setElementFrozen,180000, 1, false)
				--Start timer on clients screen
				--After, give the item
				outputChatBox("You have succesfully made some methamphetamine!", getRootElement(), 10,255,0,false)
				else outputChatBox("You don't have the required ingredients to cook methamphetamine!", 10,255,0,false)
			end
		end
	else nil
	end
end
addEventHandler ("onVehicleEnter", getRootElement(), giveoptions)
addCommandHandler("cookmeth", cook)

 

I'll be helping you a bit here: fix ur most obvious mistakes.
First of all, you used two functions in one. That's not possible. Let's just remove that one ''function cook''.
Second, I see you have ''function giveoptions (theplayer) and then u use ''player'' or ''getRootElement() everywhere. That doesn't work, u have to use only one.
So, change giveoptions(thePlayer) to giveoptions(player) so it all makes sense again.
Then, addCommandHandler("cookmeth") is correct, but '', cook)'' is not. You have to put the name of the function there. Change it to addCommandHandler("cookmeth"), because using a variable is not necessary because the only variable that can do a command is a player.
Do that, then tell me if u need any more help. Good luck with ur script :)

(u can also add me on skype @ marty55501, ill be glad to help)

 

Edited by marty000123
Link to comment

my two cents:

local TIME_TO_PREPARE = 3*60*1000 --3m
local TICK_DURATION = 500 --tick every 500ms
local DAMAGE_PER_TICK = 1 --DPS = DAMAGE_PER_TICK*(1000/TICK_DURATION) --just an example

local methTimers = {}

function cook(thePlayer, cmd, ...)
	if getElementModel(getPedOccupiedVehicle( thePlayer )) ~= 508 then return end --not in Journey
	if not hasPlayerTheIngredients(thePlayer) then outputChatBox( "No ingredients!", thePlayer ) return end --check if he has ingredients
	if methTimers[thePlayer] and isTimer( methTimers[thePlayer][1] ) then outputChatBox( "Already cooking meth!", thePlayer ) return end
	methTimers[thePlayer] = {}
	methTimers[thePlayer][1] = setTimer( cookMethTick, TICK_DURATION, math.floor(TIME_TO_PREPARE/TICK_DURATION), thePlayer )
	setElementFrozen( thePlayer, true )
	methTimers[thePlayer][2] = setTimer( setElementFrozen, TIME_TO_PREPARE, 1, thePlayer, false )
	--if you want, remove ingredients here
end
addCommandHandler( "cookmeth", cook, false, false )


function cookMethTick(thePlayer)
	if hasPlayerTheGasMask(thePlayer) then
		setElementHealth( thePlayer, getElementHealth( thePlayer ) - DAMAGE_PER_TICK )
	end
	local _, tickLeft, _ = getTimerDetails ( methTimers[thePlayer] )
	if tickLeft == 0 then --last tick
		--use inventory to add meth or wathever you want (remove the ingredients if you didn't at start!)
		--also clear the methTimers table
		methTimers[thePlayer] = nil
	end
end

function hasPlayerTheIngredients( thePlayer )
	--use exported functions/data provided from the inventory script
	return true or false
end

function hasPlayerTheGasMask( thePlayer )
	--use exported functions/data provided from the script that handles gas masks
	return true or false
end




function alertPlayerEnteringJourney(theVehicle)
	if getElementModel(getPedOccupiedVehicle( thePlayer )) ~= 508 then return end --not in Journey
	outputChatBox( "You can cook meth! /cookmeth", source )
end
addEventHandler( "onPlayerVehicleEnter", root, alertPlayerEnteringJourney )


--explain more the point:
--At the same time display a countdown of the 3 minutes saying "3 minutes of cooking remaining" or something like that that goes down.
--DX?

UNTESTED
I'm also not sure about the getTimerDetails part, as wiki is not much helpful
PS: 3 minutes? what if other players starts attacking you? Should you stay frozen watching you beign killed?

 

Edited by LoPollo
  • Like 1
Link to comment
12 minutes ago, LoPollo said:

my two cents:


local TIME_TO_PREPARE = 3*60*1000 --3m
local TICK_DURATION = 500 --tick every 500ms
local DAMAGE_PER_TICK = 1 --DPS = DAMAGE_PER_TICK*(1000/TICK_DURATION) --just an example

local methTimers = {}

function cook(thePlayer, cmd, ...)
	if getElementModel(getPedOccupiedVehicle( thePlayer )) ~= 508 then return end --not in Journey
	if not hasPlayerTheIngredients(thePlayer) then outputChatBox( "No ingredients!", thePlayer ) return end --check if he has ingredients
	if methTimers[thePlayer] and isTimer( methTimers[thePlayer][1] ) then outputChatBox( "Already cooking meth!", thePlayer ) return end
	methTimers[thePlayer] = {}
	methTimers[thePlayer][1] = setTimer( cookMethTick, TICK_DURATION, math.floor(TIME_TO_PREPARE/TICK_DURATION), thePlayer )
	setElementFrozen( thePlayer, true )
	methTimers[thePlayer][2] = setTimer( setElementFrozen, TIME_TO_PREPARE, 1, thePlayer, false )
	--if you want, remove ingredients here
end
addCommandHandler( "cookmeth", cook, false, false )


function cookMethTick(thePlayer)
	if hasPlayerTheGasMask(thePlayer) then
		setElementHealth( thePlayer, getElementHealth( thePlayer ) - DAMAGE_PER_TICK )
	end
	local _, tickLeft, _ = getTimerDetails ( methTimers[thePlayer] )
	if tickLeft == 0 then --last tick
		--use inventory to add meth or wathever you want (remove the ingredients if you didn't at start!)
		--also clear the methTimers table
		methTimers[thePlayer] = nil
	end
end

function hasPlayerTheIngredients( thePlayer )
	--use exported functions/data provided from the inventory script
	return true or false
end

function hasPlayerTheGasMask( thePlayer )
	--use exported functions/data provided from the script that handles gas masks
	return true or false
end




function alertPlayerEnteringJourney(theVehicle)
	if getElementModel(getPedOccupiedVehicle( thePlayer )) ~= 508 then return end --not in Journey
	outputChatBox( "You can cook meth! /cookmeth", source )
end
addEventHandler( "onPlayerVehicleEnter", root, alertPlayerEnteringJourney )


--explain more the point:
--At the same time display a countdown of the 3 minutes saying "3 minutes of cooking remaining" or something like that that goes down.
--DX?

UNTESTED

PS: 3 minutes? what if other players starts attacking you? Should you stay frozen watching you beign killed?

 

You most likely nailed it exactly,

BUT MY AIM ISN'T TO GET A SERVER LEVEL SCRIPT TO USE!

My aim is to learn how to do this all, so no offence, but I didn't learn almost crap from this xD

Would you be willing to get in contact or something and possibly explain this and/or teach me a bit?

Edited by lopexsw
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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