boro Posted November 26, 2014 Share Posted November 26, 2014 Hi i make noob zombie script this script is for set zombie health to 50 if is zombie skin 281 It work good, but if is zombie damaged and is spawned next zombie then old damaged zombie have set 50 health back why ? Please Help addEvent ( "onZombieSpawn", true ) addEventHandler ( "onZombieSpawn", root, function () for _, v in ipairs ( getElementsByType ( "ped" ) ) do if ( getElementModel ( v ) == 281 ) then setElementHealth ( v, 50 ) outputChatBox("noob zombie spawned") end end end ) Link to comment
Moderators IIYAMA Posted November 26, 2014 Moderators Share Posted November 26, 2014 addEvent ( "onZombieSpawn", true ) addEventHandler ( "onZombieSpawn", root, function () if getElementModel ( source ) == 281 then setElementHealth ( source, 50 ) outputChatBox("noob zombie spawned") end end) Because you are using a loop? Link to comment
boro Posted November 26, 2014 Author Share Posted November 26, 2014 now it dont work and chatbox dont work too and i try this way but without loop this dont work Link to comment
Moderators IIYAMA Posted November 26, 2014 Moderators Share Posted November 26, 2014 If the ped is the source is another question, it can also be an argument. You should debug your variables to find that answer. You know what a variable is? and what arguments are? Link to comment
boro Posted November 26, 2014 Author Share Posted November 26, 2014 Idk what you mean, my previous script work, but why it work for all zombies and not only for one spawned zombie this is what i dont know .. and i try it with ped, zombie, source and still dont work Link to comment
Moderators IIYAMA Posted November 26, 2014 Moderators Share Posted November 26, 2014 ah common, you copied those last few lines from somebody else. Else you would not have used that loop, which is doing exactly what you don't want. Your other solution: - Find the source code(resource lines) of this event, then we have all information you need to make it. The function which is used is: triggerEvent If you aren't able to debug your code by debugging the variables and you aren't able to find the line that is firing this event, I don't have enough information to help you out on this. Link to comment
Ahmed Abo-elezz Posted November 26, 2014 Share Posted November 26, 2014 you need to client side triggerserverEvent ( "", root ) Link to comment
Dealman Posted November 27, 2014 Share Posted November 27, 2014 addEvent("onZombieSpawn", true) addEventHandler("onZombieSpawn", root, function () for _, v in ipairs(getElementsByType("ped")) do if(getElementModel(v) == 281) then setElementHealth(v, 50) outputChatBox("noob zombie spawned") end end end) What you're doing here is that every time a zombie is spawned, you get ALL elements by the type Ped. This means, that any and all peds(zombies) will be stored in a table. You then created a for loop, which runs through every index in the table and set their health to 50. Thus, every time a zombie is spawned - you will repeat this procedure, setting all the zombie's health to 50. You can use this solution, albeit in a different way - using Element Data. Since Element Data is a very powerful tool at your disposal, I'll provide you with an example of how to use it along with your above solution. Do note, this is not the optimal solution - just one of the possible ones. Example; (I apologize in advance if I made some errors, didn't test it) function setZombieSpawnState_Handler() local allZombies = getElementsByType("ped"); for _, thePed in ipairs(allZombies) do if(getElementModel(thePed) == 281) then local theData = getElementData(thePed, "NoobZombie.HealthState"); -- Couldn't come up with any better name for it... if(theData == false) then setElementData(thePed, "NoobZombie.HealthState", true); setElementHealth(thePed, 50); end end end end addEvent("onZombieSpawn", true); addEventHandler("onZombieSpawn", root, setZombieSpawnState_Handler); The logic behind this solution is that by element datas are set to false by default if it has not already been set or contains no value. Thus, you can use this to your advantage to check if the health has already been set to 50 before or not. addEvent ( "onZombieSpawn", true ) addEventHandler ( "onZombieSpawn", root, function () if getElementModel ( source ) == 281 then setElementHealth ( source, 50 ) outputChatBox("noob zombie spawned") end end) Most likely why his solution did not work is because source is returning nil. It is important that you learn to debug your script. For example you can use the tostring function to see what source is returning; outputChatBox("Source: ["..tostring(source).."]"); --If it returns Userdata, you can do this outputChatBox("SourceType: ["..tostring(getElementType(source)).."]"); I would need to see how the event onZombieSpawn is triggered to further help you with a solution similar to the one he suggested(it would be more effiecent). Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now