Jump to content

[HELP] Triggering, getElementData and radio button problem


..:D&G:..

Recommended Posts

Hello guys, I am working on an exchange-system and I want to make a machine that exchanges coins for money. When a player selects the amount of coins they want to exchange, I get an error saying that getPlayerCoins retuns nil, even tho when I made a function with a command and chatbox, it returned my number of coins.

function exchangeOption1(thePlayer) 
    local playerCoins = tonumber(getPlayerCoins(thePlayer)) 
    if (playerCoins > 99) then 
        givePlayerMoney(thePlayer, 1) 
        takePlayerCoins(thePlayer, 100) 
    else  
        outputChatBox("You don't have enough coins!", thePlayer, 255, 255, 0) 
    end 
end 
addEvent("exchangeOption1", true) 
addEventHandler("exchangeOption1", getRootElement(), exchangeOption1) 

Client side -

function proceedCEM( button, state ) 
    if ( button == "left" and state == "up" ) then 
        if (guiRadioButtonGetSelected(option1)) then 
            triggerServerEvent("exchangeOption1", getLocalPlayer()) 
            closeCEM() 
        elseif (guiRadioButtonGetSelected(option2)) then 
            triggerServerEvent("exchangeOption2", getLocalPlayer()) 
            closeCEM() 
        elseif (guiRadioButtonGetSelected(option3)) then 
            triggerServerEvent("exchangeOption3", getLocalPlayer()) 
            closeCEM() 
        elseif (guiRadioButtonGetSelected(option4)) then 
            triggerServerEvent("exchangeOption4", getLocalPlayer()) 
            closeCEM() 
        elseif (guiRadioButtonGetSelected(option5)) then 
            triggerServerEvent("exchangeOption5", getLocalPlayer()) 
            closeCEM() 
        elseif (guiRadioButtonGetSelected(option6)) then 
            triggerServerEvent("exchangeOption6", getLocalPlayer()) 
            closeCEM() 
        else 
            showPINCodeError("You did not select an option.") 
        end 
    end 
end      

Any ideas guys?

Link to comment

You never send a player argument, you just specify it as a source element.

function exchangeOption1() 
    local playerCoins = tonumber(getPlayerCoins(source)) 
    if (playerCoins > 99) then 
        givePlayerMoney(source, 1) 
        takePlayerCoins(source, 100) 
    else 
        outputChatBox("You don't have enough coins!", source, 255, 255, 0) 
    end 
end 
addEvent("exchangeOption1", true) 
addEventHandler("exchangeOption1", getRootElement(), exchangeOption1) 

This should fix your current problem.

However, it's vulnerable to event faking. You should replace thePlayer from triggerServerEvent with root, and then use 'client' on the server. Read more here: https://wiki.multitheftauto.com/wiki/TriggerServerEvent

Link to comment

My bad, I forgot to remove the parameter.

Try this:

function exchangeOption1() 
    local playerCoins = tonumber(getPlayerCoins(source)) 
    if (playerCoins > 99) then 
        givePlayerMoney(source, 1) 
        takePlayerCoins(source, 100) 
    else 
        outputChatBox("You don't have enough coins!", source, 255, 255, 0) 
    end 
end 
addEvent("exchangeOption1", true) 
addEventHandler("exchangeOption1", getRootElement(), exchangeOption1) 

Link to comment

Same thing...

This is what I done for the client side, maybe is something wrong with it?

function proceedCEM( button, state ) 
    if ( button == "left" and state == "up" ) then 
        if (guiRadioButtonGetSelected(option1)) then 
            triggerServerEvent("exchangeOption1", root) 
            closeCEM() 

Link to comment

This is why I separated my answer, so that you don't get confused, I guess it didn't help.

Now 'source' will not refer to the player, you need to use 'client'. If what you posted is your current client code then this should work:

function exchangeOption1() 
    local playerCoins = tonumber(getPlayerCoins(client)) 
    if (playerCoins > 99) then 
        givePlayerMoney(client, 1) 
        takePlayerCoins(client, 100) 
    else 
        outputChatBox("You don't have enough coins!", client, 255, 255, 0) 
    end 
end 
addEvent("exchangeOption1", true) 
addEventHandler("exchangeOption1", getRootElement(), exchangeOption1) 

Link to comment
  • MTA Team
function exchangeOption1() 
    local playerCoins = tonumber(getPlayerCoins(client)) 
    if (playerCoins > 99) then 
        givePlayerMoney(client, 1) 
        takePlayerCoins(client, 100) 
    else 
        outputChatBox("You don't have enough coins!", client, 255, 255, 0) 
    end 
end 

Use the hidden variable client

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