Jump to content

onzombiesapwn help


boro

Recommended Posts

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
    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
  • Moderators

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

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