Jump to content

I Need help


Danz

Recommended Posts

I am attempting to make a drug system to release on the MTA Community but there are some bugs which i cannot for the life of me find, here is the code;

function onGUIChange () 
    if drugDealer then 
        if getElementData(drugDealer, "DrugsDealer") then 
            MaxHeroin = (getElementData(drugDealer, "Heroin") or 0) + (getElementData(localPlayer, "Heroin") or 0) 
            MaxSteroids = (getElementData(drugDealer, "Steroids") or 0) + (getElementData(localPlayer, "Steroids") or 0) 
            MaxGod = (getElementData(drugDealer, "God") or 0) + (getElementData(localPlayer, "God") or 0) 
            MaxWeed = (getElementData(drugDealer, "Weed") or 0) + (getElementData(localPlayer, "Weed") or 0) 
            MaxSpeed = (getElementData(drugDealer, "Speed") or 0) + (getElementData(localPlayer, "Speed") or 0) 
            MaxCrack = (getElementData(drugDealer, "Crack") or 0) + (getElementData(localPlayer, "Crack") or 0) 
        else 
            outputChatBox("This Player is not selling drugs anymore, sorry", 255, 0, 0) 
            drugDealer = nil 
            guiSetVisible(window[2], false) 
            showCursor(false) 
        end 
    else 
        --Change this to the maximum drug amount that you can carry for each type of drug 
        MaxHeroin = 5000000 
        MaxSteroids = 5000000 
        MaxGod = 5000000 
        MaxWeed = 5000000 
        MaxSpeed = 5000000 
        MaxCrack = 5000000 
    end 
    local Text = guiGetText(source) 
    local Text = tonumber(Text) 
    if source == edit[1] then 
        local Amount = getElementData(localPlayer, "Heroin") or 0 
        if not Text then 
            guiSetText(source, "0") 
        elseif Text+Amount > MaxHeroin then 
            guiSetText(edit[1], MaxHeroin-Amount >= 0 and MaxHeroin-Amount or "0") 
        end 
        HeroinCost = tonumber(guiGetText(source)) * tonumber(string.sub(guiGetText(label[8]), 2)) 
    elseif source == edit[2] then 
        local Amount = getElementData(localPlayer, "Steroids") or 0 
        if not Text then 
            guiSetText(source, "0") 
        elseif Text+Amount > MaxSteroids then 
            guiSetText(edit[2], MaxSteroids-Amount >= 0 and MaxSteroids-Amount or "0") 
        end 
        SteroidsCost = tonumber(guiGetText(source)) * tonumber(string.sub(guiGetText(label[10]), 2)) 
    elseif source == edit[3] then 
        local Amount = getElementData(localPlayer, "God") or 0 
        if not Text then 
            guiSetText(source, "0") 
        elseif Text+Amount > MaxGod then 
            guiSetText(edit[3], MaxGod-Amount >= 0 and MaxGod-Amount or "0") 
        end 
        GodCost = tonumber(guiGetText(source)) * tonumber(string.sub(guiGetText(label[12]), 2)) 
    elseif source == edit[4] then 
        local Amount = getElementData(localPlayer, "Weed") or 0 
        if not Text then 
            guiSetText(source, "0") 
        elseif Text+Amount > MaxWeed then 
            guiSetText(edit[4], MaxWeed-Amount >= 0 and MaxWeed-Amount or "0") 
        end 
        WeedCost = tonumber(guiGetText(source)) * tonumber(string.sub(guiGetText(label[14]), 2)) 
    elseif source == edit[5] then 
        local Amount = getElementData(localPlayer, "Speed") or 0 
        if not Text then 
            guiSetText(source, "0") 
        elseif Text+Amount > MaxSpeed then 
            guiSetText(edit[5], MaxSpeed-Amount >= 0 and MaxSpeed-Amount or "0") 
        end 
        SpeedCost = tonumber(guiGetText(source)) * tonumber(string.sub(guiGetText(label[16]), 2)) 
    elseif source == edit[6] then 
        local Amount = getElementData(localPlayer, "Crack") or 0 
        if not Text then 
            guiSetText(source, "0") 
        elseif Text+Amount > MaxCrack then 
            guiSetText(edit[6], MaxCrack-Amount >= 0 and MaxCrack-Amount or "0") 
        end 
        CrackCost = tonumber(guiGetText(source)) * tonumber(string.sub(guiGetText(label[18]), 2)) 
    end 
    local TotalCost = (HeroinCost or 0) + (SteroidsCost or 0) + (GodCost or 0) + (WeedCost or 0) + (SpeedCost or 0) + (CrackCost or 0) 
    guiSetText(label[20], convertNumber(TotalCost)) 
end 
addEventHandler("onClientGUIChanged", guiRoot, onGUIChange) 
  
function onClickDrugPurchase () 
    if source == button[3] then 
        local HeroinAmount = tonumber(guiGetText(edit[1])) 
        local SteroidsAmount = tonumber(guiGetText(edit[2])) 
        local GodAmount = tonumber(guiGetText(edit[3])) 
        local WeedAmount = tonumber(guiGetText(edit[4])) 
        local SpeedAmount = tonumber(guiGetText(edit[5])) 
        local CrackAmount = tonumber(guiGetText(edit[6])) 
        local Cost = tonumber(string.sub(guiGetText(label[20]), 2)) 
        if Cost > 0 then 
            if drugDealer then 
                if getElementData(drugDealer, "DrugsDealer") then 
                    MaxHeroin = getElementData(drugDealer, "Heroin") or 0 
                    MaxSteroids = getElementData(drugDealer, "Steroids") or 0 
                    MaxGod = getElementData(drugDealer, "God") or 0 
                    MaxWeed = getElementData(drugDealer, "Weed") or 0 
                    MaxSpeed = getElementData(drugDealer, "Speed") or 0 
                    MaxCrack = getElementData(drugDealer, "Crack") or 0 
                    if MaxHeroin >= HeroinAmount and MaxSteroids >= SteroidsAmount and MaxGod >= GodAmount and MaxWeed >= WeedAmount and MaxSpeed >= SpeedAmount and MaxCrack >= CrackAmount then 
                        triggerServerEvent("purchaseDrugs", localPlayer, Cost, HeroinAmount, SteroidsAmount, GodAmount, WeedAmount, SpeedAmount, CrackAmount, drugDealer) 
                    else 
                        outputChatBox("This drug dealer does not have enough drugs for ya", 255, 0, 0) 
                    end 
                else 
                    outputChatBox("This Player is not selling drugs anymore, sorry", 255, 0, 0) 
                    drugDealer = nil 
                    guiSetVisible(window[2], false) 
                    showCursor(false) 
                end 
            else 
                if MaxHeroin >= HeroinAmount and MaxSteroids >= SteroidsAmount and MaxGod >= GodAmount and MaxWeed >= WeedAmount and MaxSpeed >= SpeedAmount and MaxCrack >= CrackAmount then 
                    triggerServerEvent("purchaseDrugs", localPlayer, Cost, HeroinAmount, SteroidsAmount, GodAmount, WeedAmount, SpeedAmount, CrackAmount, false) 
                end 
            end 
            guiSetText(label[19], "Total Cost: ") 
            guiSetText(edit[1], "0") 
            guiSetText(edit[2], "0") 
            guiSetText(edit[3], "0") 
            guiSetText(edit[4], "0") 
            guiSetText(edit[5], "0") 
            guiSetText(edit[6], "0") 
        end 
    end 
end 
addEventHandler("onClientGUIClick", guiRoot, onClickDrugPurchase) 

When i attempt to buy Heroin and Weed it just doesn't do anything, when i attempt to buy Crack AND Speed, it just says in the debug, "attempt to compare number with nil", and when i buy Steroids and God, it works as it triggers the server-side event (even tho it is not added yet, but works), thank you if you can help me

Edited by Guest
Link to comment
it just says in the debug, "attempt to compare number with nil", and when i buy Steroids and God, it works as it triggers the server-side event

What line is this error on?

EDIT: It's line 90 in the code i posted, and the error occurs when i attempt to buy Speed AND Crack together

Edited the main post, sorry if i confused you, 226 is the bugged line WITH THE GUI, but this code doesn't have the GUI so, sorry

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