Jump to content

ERROR: attempt to compare number with nil


unkwn

Recommended Posts

Hello. There is an error in the script. What could be the problem?
ERROR: [gameplay]/zombies/zombie_server. Lua:428: attempt to compare number with nil

 

addEvent( "onZombieSpawn", true )
function RanSpawn_Z ( gx, gy, gz, rot, zombieType, row )
    local safezone = 0
    local allradars = getElementsByType("radararea")
    for theKey,theradar in ipairs(allradars) do
        if getElementData(theradar, "zombieProof") == true then
            if isInsideRadarArea ( theradar, gx, gy ) then
                safezone = 1
            end
        end
    end
    if safezone == 0 then
        if table.getn ( everyZombie ) < newZombieLimit then   --428 line
            if not rot then
                rot = math.random (1,359)
            end
      
            local ZombiePedSkins = false;

            if (zombieType == 1) or (zombieType == 2) then
                ZombiePedSkins = {10}
            elseif (zombieType == 3) then
                ZombiePedSkins = {11}
            end
            
            if (ZombiePedSkins) then
              local randomZskin = math.random ( 1, table.getn ( ZombiePedSkins ) )				
              local zomb = createPed( tonumber( ZombiePedSkins[randomZskin] ), gx, gy, gz )
 
              if (zomb ~= false) then
                setElementData( zomb, "zombie:type", zombieType )
                if zombieType == 1 then
                    setElementData( zomb, "head:n", 1)
                    setElementData( zomb, "head:now", 0 )
                elseif zombieType == 2 then
                    exports.extra_health:setElementExtraHealth ( zomb, 400 )
                    setElementData( zomb, "head:n", 3 )
                    setElementData( zomb, "head:now", 0 )
                elseif zombieType == 3 then
                    setElementData( zomb, "respawn:zombie", row )
                    exports.extra_health:setElementExtraHealth ( zomb, 800 )
                end

                setElementData ( zomb, "zombie", true  )
                table.insert( everyZombie, zomb )	
                setTimer ( function (zomb, rot) if ( isElement ( zomb ) ) then setPedRotation ( zomb, rot ) end end, 500, 1, zomb, rot )
                local chaseanim, checkspeed = getZombieType ( zomb )
                setTimer ( function (zomb) if ( isElement ( zomb ) ) then setPedAnimation ( zomb, "ped", chaseanim, -1, true, true, true ) end end, 1000, 1, zomb )
                setTimer ( function (zomb) if ( isElement ( zomb ) ) then setElementData ( zomb, "status", "idle" ) end end, 2000, 1, zomb )
                triggerClientEvent ( "Zomb_STFU", getRootElement(), zomb )
              end
            end
          end
        end
    end
addEventHandler( "onZombieSpawn", getRootElement(), RanSpawn_Z )

 

Link to comment

Well that's why the error pops up. You cannot compare an undefined values. You need to define everyZombie and newZombieLimit  at the top (or another appropriate place) of the script.

everyZombie = {} -- no (empty table) zombies at beginning
newZombieLimit = 100 -- or some other value

 

Link to comment

Yes, I have already made this definition. Thanks. But after the fix, another error occurred: server triggered clientside event Zomb_STFU, but event is not added clientside. Although I seem to have added an event: 

addEvent( "Zomb_STFU", true )
function Zstfu ( ped )
	if (isElement(ped)) then
		setPedVoice(ped, "PED_TYPE_DISABLED")
	end
end
addEventHandler( "Zomb_STFU", getRootElement(), Zstfu )

 

Edited by unkwn
Link to comment
  • Moderators
58 minutes ago, unkwn said:

Yes, I have already made this definition. Thanks. But after the fix, another error occurred: server triggered clientside event Zomb_STFU, but event is not added clientside. Although I seem to have added an event: 


addEvent( "Zomb_STFU", true )
function Zstfu ( ped )
	if (isElement(ped)) then
		setPedVoice(ped, "PED_TYPE_DISABLED")
	end
end
addEventHandler( "Zomb_STFU", getRootElement(), Zstfu )

 

It occurs when you use triggerClientEvent on a player that isn't finished downloading or loading it's scripts. In most cases it can be ignored. In your case it means that your zombie isn't muted for a specific player.

If you want to optimise it, you should keep track of loaded players and only send messages to that segment. And also send a message to new loaded players.

It is a time consuming fix, unless it was correctly done from the beginning.

Edited by IIYAMA
Link to comment
  • Moderators
3 hours ago, unkwn said:

Can you tell us more about tracking loaded players

The event onClientResourceStart tells the client/player that a resource has been started on his computer.

Then you can use triggerServerEvent directly after that to inform the server that a resource has been loaded on a client his computer.

By adding these players to a table, you specify which players are loaded:

local loadedPlayers = {}

 

loadedPlayers[#loadedPlayers + 1] = player

 

triggerClientEvent ( loadedPlayers, "eventName", resourceRoot)

 

 

 

3 hours ago, unkwn said:

and sending messages to this segment?

This only matters when an instruction send in the past is relevant momentarily and can't it be predicted without synchronizing with the server.

 

So the instruction is: the server tells a player that a ped has to be muted.

But a specific player didn't load his script. So he isn't aware of that instruction.

 

The player loads from this moment his scripts.
The following question is: Does this instruction to mute a ped, is relevant for this player? Because the ped isn't muted for him.

 

Based on the reaction on your previous reply, it is a YES.

 

Can this player that didn't receive the instruction, predict the instruction that should have been executed on the ped?

A: If all peds have to be muted within the resource. Then yes, you can predict this instruction and disable clientside the voices of all peds of this resource.
Note: something similar might have been added to this resource already.

addEventHandler("onClientResourceStart", resourceRoot, 
function () 
	setPedVoice(resourceRoot, "PED_TYPE_DISABLED") -- mute all current peds of this resource. This does not include new created once.
end)

B: If that is not the case, then I do not have 1,2,3 a solution for you. You need to keep track which peds are muted and which are not.

 

 

 

 

 

 

 

 

 

 

Edited by IIYAMA
  • Like 1
Link to comment

Alternatively (a simpler solution that doesn't require tracking loaded players) you can simply have the server tell the client the list of peds to mute when the client is ready and asks for it. That is, [client] -> onClientResurceStart -> triggerServerEvent informing server that client is ready -> [server] -> triggerClientEvent with list of zombies -> [client] -> setPedVoice for each zombie received. This fixes zombies not being muted if the event was received before the client was ready, but it won't suppress the warnings about sending events before the client is ready - for that, you will need to track whether the client is ready before triggering the events.

Edited by Addlibs
  • 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...