1

This script allows people to produce meth, starting production will succeed when you have the right amount of objects in your inventory. Only when you don't have the right amount you should get a notification and you don't get that notification.. Following error pops up: SCRIPT ERROR: @ns-meth/server.lua:18: Attempt to index a nil value.

Code:

RSCore = nil

Citizen.CreateThread(function()
    while RSCore == nil do
        TriggerEvent('RSCore:GetObject', function(obj) RSCore = obj end)
        Citizen.Wait(0)
    end
end)

RegisterServerEvent('RSCore_methcar:start')
AddEventHandler('RSCore_methcar:start', function()
    local src = source
    local Player = RSCore.Functions.GetPlayer(src)
    local amount = 0
    
    if Player.Functions.GetItemByName('acetone').amount >= 5 and Player.Functions.GetItemByName('lithium').amount >= 2 and Player.Functions.GetItemByName('methlab').amount >= 1 then
        
        TriggerClientEvent('RSCore_methcar:startprod', src)
        Player.Functions.RemoveItem('acetone', 5)
        Player.Functions.RemoveItem('lithium', 2)
    
    else 
        if Player.Functions.GetItemByName('acetone').amount <= 4 and Player.Functions.GetItemByName('lithium').amount <= 1 and Player.Functions.GetItemByName('methlab').amount <= 0 then
            RSCore.Functions.Notify("Je hebt niet de juiste benodigdheden!", "error")
        end
    end
end)

RegisterServerEvent('RSCore_methcar:stopf')
AddEventHandler('RSCore_methcar:stopf', function(id)
local src = source
    local Players = RSCore.GetPlayers()
    local Player = RSCore.Functions.GetPlayer(src)
    for i=1, #Players, 1 do
        TriggerClientEvent('RSCore_methcar:stopfreeze', Players[i], id)
    end
    
end)
RegisterServerEvent('RSCore_methcar:make')
AddEventHandler('RSCore_methcar:make', function(posx,posy,posz)
    local src = source
    local Player = RSCore.Functions.GetPlayer(src)
    
    if Player.Functions.GetItemByName('methlab').amount >= 1 then
    
        local Players = RSCore.Functions.GetPlayer(src)
        for i=1, #Players, 1 do
            TriggerClientEvent('RSCore_methcar:smoke',Players[i],posx,posy,posz, 'a') 
        end
        
    else
        TriggerClientEvent('RSCore_methcar:stop', src)
    end
    
end)
RegisterServerEvent('RSCore_methcar:finish')
AddEventHandler('RSCore_methcar:finish', function(qualtiy)
    local src = source
    local Player = RSCore.Functions.GetPlayer(src)
    print(qualtiy)
    local rnd = math.random(-5, 5)
    TriggerEvent('KLevels:addXP', src, 20)
    Player.Functions.AddItem('meth', math.floor(qualtiy / 2) + rnd)
    
end)

RegisterServerEvent('RSCore_methcar:blow')
AddEventHandler('RSCore_methcar:blow', function(posx, posy, posz)
    local src = source
    local Players = RSCore.GetPlayers()
    local Player = RSCore.Functions.GetPlayer(src)
    for i=1, #Players, 1 do
        TriggerClientEvent('RSCore_methcar:blowup', Players[i],posx, posy, posz)
    end
    Player.removeInventoryItem('methlab', 1)
end)

RegisterServerEvent('ns-meth:server:callCops')
AddEventHandler('ns-meth:server:callCops', function(streetLabel, coords)
    local msg = "Er is een verdachte situatie op "..streetLabel..", mogelijks drugs productie."
    local alertData = {
        title = "Verdachte situatie",
        coords = {x = coords.x, y = coords.y, z = coords.z},
        description = msg
    }
    for k, v in pairs(RSCore.Functions.GetPlayers()) do
        local Player = RSCore.Functions.GetPlayer(v)
        if Player ~= nil then 
            if (Player.PlayerData.job.name == "police" and Player.PlayerData.job.onduty) then
                TriggerClientEvent("ns-meth:client:robberyCall", Player.PlayerData.source, msg, streetLabel, coords)
                TriggerClientEvent("rs-phone:client:addPoliceAlert", Player.PlayerData.source, alertData)
            end
        end
    end
end)
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
Jelle
  • 11
  • 2
  • 2
    please provide full information from the error. On what line does it error, does it have lines with filenames after this line (stacktrace)? – Spar Nov 18 '20 at 20:21
  • Some of your functions gives you `nil` instead of table. Check if `Player.Functions.GetItemByName('lithium')` gives you table or nil back if your character has no lithium at all. – Darius Nov 18 '20 at 20:37
  • I edited it, now you can see the whole code and the error on line 18. – Jelle Nov 18 '20 at 20:40
  • Where did you get `source` from? – Darius Nov 18 '20 at 20:41
  • Then `Player.Functions.GetItemByName(itemname)` gives you nil instead of table. You need to check for nil and then get field amount or add extra function like `function getAmount(name) return Player.Functions.GetItemByName(name) and Player.Functions.GetItemByName(name).amount or 0 end` and use this function instead of amount directly – Darius Nov 18 '20 at 20:54
  • Thanks, i will try to do that. – Jelle Nov 18 '20 at 20:59
  • Problem solved, thanks for the information @Darius! – Jelle Nov 18 '20 at 23:38

0 Answers0