Jump to content

[HELP]createPed is not working !


Recommended Posts

Hello guys! I wnt to make a Ped , with Name, Tags etc. but the script says error :/. 

Debugscript says:ERROR:mta_shootingrange/sourceC.lua : 14 : attempt to index global 'ped'(a nil value)

 

Here is the script:

local pedPos = {
 {1539.3648681641, -1354.3519287109, 329.46493530273, 0, 0, 180, "Jacky", 10},
}

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function ()
 for index, value in ipairs (pedPos) do
  if isElement(pedPos[index]) then
   destroyElement(pedPos[index])
  else
   ped[index] = createPed(value[8], value[1], value[2], value[3])
   setElementRotation(ped[index], 0, 0, 90)
   ped[index]:setData("ped:shooting", true)
   ped[index]:setData("ped:shooting", true)
   setElementFrozen(ped[index], true)
   ped[index]:setData("ped:name", value[7])
   ped[index]:setData("name:tags", "Shooting Range")
  end
 end
end)
Link to comment
  • Moderators

Try this (check the comments):

local ped = {} -- created the empty list to store peds
local pedPos = {
    {1539.3648681641, -1354.3519287109, 329.46493530273, 0, 0, 180, "Jacky", 10},
}

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function ()
    for index, value in ipairs (pedPos) do
        if isElement(ped[index]) then -- we want to get a possible ped from ped table not pedPos
            destroyElement(ped[index]) -- same here
        else
            ped[index] = createPed(value[8], value[1], value[2], value[3])
            setElementRotation(ped[index], 0, 0, 90)
            ped[index]:setData("ped:shooting", true)
            ped[index]:setData("ped:shooting", true)
            setElementFrozen(ped[index], true)
            ped[index]:setData("ped:name", value[7])
            ped[index]:setData("name:tags", "Shooting Range")
        end
    end
end)

 

Link to comment
  1. local monitorSize = {guiGetScreenSize()}
    local panelSize = {300, 350}
    local panelPos = {monitorSize[1]/2-panelSize[1]/2, monitorSize[2]/2-panelSize[2]/2}
    local pedPos = {}
    local pedPos = {
     {1539.3648681641, -1354.3519287109, 329.46493530273, 0, 0, 180, "Jacky", 10},
    }
    local weapons= {{"M4"}, {"AK-47"}, {"MP5"}, {"UZI/Tec-9"}, {"Colt-45"}, {"Desert Eagle"}, {"Silenced Colt"}, {"Sörétes"}}
    local Font1 = dxCreateFont("files/Roboto.ttf", 11)
    show = false
    addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function ()
     for index, value in ipairs (pedPos) do
      if isElement(pedPos[index]) then
       destroyElement(pedPos[index])
      else
       pedPos[index] = createPed(value[8], value[1], value[2], value[3])
       setElementRotation(pedPos[index], 0, 0, 90)
       pedPos[index]:setData("ped:shooting", true)
       pedPos[index]:setData("ped:shooting", true)
       setElementFrozen(pedPos[index], true)
       pedPos[index]:setData("ped:name", value[7])
       pedPos[index]:setData("name:tags", "Shooting Range")
      end
     end
    end)
       
       
    function panelRender()
     if not show then return end
     dxDrawRectangle(panelPos[1], panelPos[2], panelSize[1], panelSize[2], tocolor(0, 0, 0, 160))
     dxDrawRectangle(panelPos[1], panelPos[2], panelSize[1], panelSize[2]-320, tocolor(0, 0, 0, 200))
     dxDrawText(" Name - #7cc576Shootingrange", panelPos[1]+580, panelPos[2]-25, panelSize[1]+100, panelSize[2]+70, tocolor(255, 255, 255, 255), 1, Font1, "center", "center", false, false, false, true)
     dxDrawText("Welcome " ..getPlayerName(localPlayer):gsub("_", " ") .. " to the Shootingrange!", panelPos[1]+10, panelPos[2]+50, panelSize[1]-100, panelSize[2]-400, tocolor(255, 255, 255, 255), 1, Font1)
     show = true
     for _, value in ipairs(weapons) do
      if value[1] == "M4" then
       dxDrawText(value[1], panelPos[1], panelPos[2], panelSize[1], panelSize[2], tocolor(255, 255, 10, 255))
      end
     end
    end
    addEventHandler("onClientRender", root, panelRender)
    function mutat()
     show = true
     if show then
      panelRender ()
     end
    end
    addCommandHandler("mutat", mutat)

that ' s the script

Edited by DarkMagic
Bad script
Link to comment
  • Moderators

Why did you revert the code we fixed for you ???

local monitorSize = {guiGetScreenSize()}
local panelSize = {300, 350}
local panelPos = {monitorSize[1]/2-panelSize[1]/2, monitorSize[2]/2-panelSize[2]/2}
local pedPos = {} -- it was ped here not pedPos
local pedPos = {
 {1539.3648681641, -1354.3519287109, 329.46493530273, 0, 0, 180, "Jacky", 10},
}
local weapons= {{"M4"}, {"AK-47"}, {"MP5"}, {"UZI/Tec-9"}, {"Colt-45"}, {"Desert Eagle"}, {"Silenced Colt"}, {"Sörétes"}}
local Font1 = dxCreateFont("files/Roboto.ttf", 11)
show = false
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function ()
 for index, value in ipairs (pedPos) do
  if isElement(pedPos[index]) then -- it was ped here not pedPos
   destroyElement(pedPos[index]) -- it was ped here not pedPos
  else
   pedPos[index] = createPed(value[8], value[1], value[2], value[3]) -- it was ped here not pedPos
   setElementRotation(pedPos[index], 0, 0, 90) -- it was ped here not pedPos
   pedPos[index]:setData("ped:shooting", true) -- it was ped here not pedPos
   pedPos[index]:setData("ped:shooting", true) -- it was ped here not pedPos
   setElementFrozen(pedPos[index], true) -- it was ped here not pedPos
   pedPos[index]:setData("ped:name", value[7]) -- it was ped here not pedPos
   pedPos[index]:setData("name:tags", "Shooting Range") -- it was ped here not pedPos
  end
 end
end)

Also use the <> button to add code.

Edited by Citizen
Link to comment

Still not working : debug says : ERROR:mta_shootingrange:SourceC.lua:22:attempt to index field '?' (a userdata value)

 

Code:

<

local monitorSize = {guiGetScreenSize()}
local panelSize = {300, 350}
local panelPos = {monitorSize[1]/2-panelSize[1]/2, monitorSize[2]/2-panelSize[2]/2}
local ped = {}
local marker = {}
local pedPos = {
 {1539.3648681641, -1354.3519287109, 329.46493530273, 0, 0, 180, "Jacky", 10},
}
local weapons= {{"M4"}, {"AK-47"}, {"MP5"}, {"UZI/Tec-9"}, {"Colt-45"}, {"Desert Eagle"}, {"Silenced Colt"}, {"Sörétes"}}
local Font1 = dxCreateFont("files/Roboto.ttf", 10)
show = false
local marker = createMarker(1539.3648681641, -1354.3519287109, 328.46493530273, "cylinder", 1, 100, 158, 100, 240, localPlayer)
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function ()
    for index, value in ipairs (pedPos) do
        if isElement(ped[index]) then -- we want to get a possible ped from ped table not pedPos
            destroyElement(ped[index]) -- same here
        else
            ped[index] = createPed(value[8], value[1], value[2], value[3])
            setElementRotation(ped[index], 0, 0, 90)
            ped[index]:setData("ped:shooting", true)
            ped[index]:setData("ped:shooting", true)
            setElementFrozen(ped[index], true)
            ped[index]:setData("ped:name", "Name")
            ped[index]:setData("name:tags", "Tag")
        end
    end
end) />
Edited by DarkMagic
.
Link to comment
  • Discord Moderators

Use the <> tag please.

Enable OOP(Object Orientated Programming) in meta.xml using the <oop>true</oop> tag.
Or just use setElementData() instead of :setData(), because using OOP only sometimes makes no sense at all.
Or if you want a fairly good looking script then you should use :setRotation() instead of setElementRotation() and use :setFrozen() instead of setElementFrozen()

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