Jump to content

[HELP] table index is nil


Mohab

Recommended Posts

hello , I have a custom animation and I made a simple script by using it.
so when you start the resource in the server and type /outr22 in chat
the player will do the custom animation


client side :

addEvent( "otur", true )
addEventHandler( "otur", root,
function(enable)
   if (enable) then setPedAnimation(source, "otur", "WEAPON_crouch", -1, true, false)
else setPedAnimation(source)
end
end
)

addEventHandler("onClientResourceStart", resourceRoot,
function()
triggerServerEvent("onClientSync", resourceRoot)
end
)

addEventHandler("onClientResourceStop", resourceRoot,
function()
if ifp then
   for _,player in ipairs(getElementsByType("player")) do
      local _, otur = getPedAnimation(player)
      if (otur == "WEAPON_crouch") then
         setPedAnimation(player)
      end
   end
   destroyElement(ifp)
end
end
)


server side : 

local animEnable = {}
local syncPlayers = {}

addCommandHandler("otur2",
function(player)
   if (not animEnable[player]) then
      animEnable[player] = true
      triggerClientEvent(syncPlayers, "otur", player, true)
      outputChatBox("[!]#FFFFFF animation enabled.", player, 0, 255, 0, true)
   else
      animEnable[player] = false
      triggerClientEvent(syncPlayers, "otur", player, false)
      outputChatBox("[!]#FFFFFF animation disabled.", player, 255, 0, 0, true)
   end
   end)

addEvent("onClientSync", true )
addEventHandler("onClientSync", resourceRoot,
function()
   outputChatBox("done")
   table.insert(syncPlayers, client)
   for player, enable in ipairs(animEnable) do
      if (enable) then
         triggerClientEvent(client, "otur", player, true)
      end
   end
end
)

addEventHandler("onPlayerQuit", root,
function()
   for i, player in ipairs(syncPlayers) do
      if source == player then
         table.remove(syncPlayers, i)
         break
      end
   end
   if (animEnable[source] == true or animEnable[source] == false) then animEnable[source] = nil end
end
)

Now I have decided to change the command you type in chat to do the animation(outr22)

from this

addCommandHandler("otur2",
function(player)
   if (not animEnable[player]) then
      animEnable[player] = true
      triggerClientEvent(syncPlayers, "otur", player, true)
      outputChatBox("[!]#FFFFFF animation enabled.", player, 0, 255, 0, true)
   else
      animEnable[player] = false
      triggerClientEvent(syncPlayers, "otur", player, false)
      outputChatBox("[!]#FFFFFF animation disabled.", player, 255, 0, 0, true)
   end
   end)

to this

addEvent("otur2",true)
addEventHandler("otur2",root,
function(player)
	if (not animEnable[player]) then
		animEnable[player] = true
		triggerClientEvent(syncPlayers, "otur", player, true)
		outputChatBox("[!]#FFFFFF animation enabled.", player, 0, 255, 0, true)
	else
		animEnable[player] = false
		triggerClientEvent(syncPlayers, "otur", player, false)
		outputChatBox("[!]#FFFFFF animation disabled.", player, 255, 0, 0, true)
	end
end)

so what I did is I changed the way to do the animation from typing a command --> to press a button to do the custom animation
clarification : when I press a button , the client side will trigger the server event which called otur2 to do the custom animation


and here's the client side :

function mouse2()
   if isMouseInPosition(1500, 240, 120, 120) and getKeyState("mouse1") then
      playSound("SFX/click.mp3")
      triggerServerEvent("otur2", localPlayer)
   end
end
addEventHandler ( "onClientClick", root, mouse2)


and When I tested the script, a problem appeared to me
ERROR: AnimationsPanel\smain.lua:9: table index is nil
NIihZIs.png


Knowing that the 9th line in server side is :
 

animEnable[player] = true


I didn't make any changes to the script As it is clear above, I just changed the command handler to event handler.
I can't figure out what's wrong or fix it, could you help me?

I hope my words are clear and I apologize if I took up too much of your time, thank you :)

Other information: The benefit of this script is to make custom animations with server side , not just client side .. it's working when I use the command way, but it's not working with event handler

Link to comment

I forgot one line at client side
 

local ifp = engineLoadIFP( "otur.ifp", "otur" )

so the client side will be :
 

Spoiler
local ifp = engineLoadIFP( "otur.ifp", "otur" )
addEvent( "otur", true )
addEventHandler( "otur", root,
function(enable)
   if (enable) then setPedAnimation(source, "otur", "WEAPON_crouch", -1, true, false)
else setPedAnimation(source)
end
end
)

addEventHandler("onClientResourceStart", resourceRoot,
function()
triggerServerEvent("onClientSync", resourceRoot)
end
)

addEventHandler("onClientResourceStop", resourceRoot,
function()
if ifp then
   for _,player in ipairs(getElementsByType("player")) do
      local _, otur = getPedAnimation(player)
      if (otur == "WEAPON_crouch") then
         setPedAnimation(player)
      end
   end
   destroyElement(ifp)
end
end
)

 

 

Link to comment

... or you can remove the first parameter from the otur2 event handler called "player" and use the global event variable source instead, since you are already passing that as second argument to the triggerServerEvent function from inside the mouse2 function.

Edited by The_GTA
  • Like 2
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...