Jump to content

Giving Money


Axel

Recommended Posts

Im trying to create a pickup near the player when he types /givemoney {ammount} , i made the script but it does not work, i can t find bugs ..

function givemoney(thePlayer, command, amount) 
    local givingplayer = getPlayerFromName ( thePlayer ) 
        local x,y,z = getElementPosition(thePlayer) 
        local x = x + 5   
        local gpickup = createPickup(x,y,z,1212) 
    takePlayerMoney (thePlayer, math.abs(tonumber(amount))) 
end 
addCommandHandler ("givemoney", givemoney) 
  
function takemoney ( thePlayer ) 
givePlayerMoney(thePlayer, math.abs(tonumber(amount))) 
destroyElement(gpickup) 
end 
addEventHandler ( "onPickupUse", gpickup, takemoney )  

Link to comment

you almost got it right good job for trying, however you do have some bugs here and there, look at my code and if there's something you don't understand just ask. You should test it too first, because I didn't.

gpickup = {} 
    
   function takemoney ( thePlayer ) 
        givePlayerMoney(thePlayer, getElementData(source, "moneyDropped")) 
        destroyElement(source) 
    end 
     
    function givemoney(thePlayer, command, amount) 
            if isElement(gpickup[thePlayer]) then return end 
            local givingplayer = getPlayerFromName ( thePlayer ) 
            local x,y,z = getElementPosition(thePlayer) 
            local x = x + 5   
            local gpickup[thePlayer] = createPickup(x,y,z,3,1212) 
            setElementData(gpickup[thePlayer], "moneyDropped", math.ceil(tonumber(amount))) 
            takePlayerMoney (thePlayer, math.abs(math.ceil(tonumber(amount)))) 
            addEventHandler('onPickupHit', gpickup[thePlayer], takemoney) 
    end 
    addCommandHandler ("givemoney", givemoney) 

Link to comment
  • 2 weeks later...

That's caused by the 'local' infront of it.

function takemoney ( thePlayer ) 
        givePlayerMoney(thePlayer, getElementData(source, "moneyDropped")) 
        destroyElement(source) 
    end 
    
    function givemoney(thePlayer, command, amount) 
            if isElement(gpickup[thePlayer]) then return end 
            local givingplayer = getPlayerFromName ( thePlayer ) 
            local x,y,z = getElementPosition(thePlayer) 
            local x = x + 5   
            gpickup[thePlayer] = createPickup(x,y,z,3,1212) 
            setElementData(gpickup[thePlayer], "moneyDropped", math.ceil(tonumber(amount))) 
            takePlayerMoney (thePlayer, math.abs(math.ceil(tonumber(amount)))) 
            addEventHandler('onPickupHit', gpickup[thePlayer], takemoney) 
    end 
    addCommandHandler ("givemoney", givemoney) 

Link to comment
local gpickup = {} 
  
function takemoney ( thePlayer ) 
    givePlayerMoney(thePlayer, getElementData(source, "moneyDropped")) 
    destroyElement(source) 
end 
    
function givemoney(thePlayer, command, amount) 
    if isElement(gpickup[thePlayer]) then return end 
    local x,y,z = getElementPosition(thePlayer) 
    local x = x + 5   
    gpickup[thePlayer] = createPickup(x,y,z,3,1212) 
    setElementData(gpickup[thePlayer], "moneyDropped", math.ceil(tonumber(amount))) 
    takePlayerMoney (thePlayer, math.abs(math.ceil(tonumber(amount)))) 
    addEventHandler('onPickupHit', gpickup[thePlayer], takemoney) 
end 
addCommandHandler ("givemoney", givemoney) 

Works fine here.

Link to comment

use this? I fixed some of the errors ( that could occur )

gpickup = {} 
function takemoney ( thePlayer ) 
    givePlayerMoney(thePlayer, getElementData(source, "moneyDropped")) 
    destroyElement(source) 
end 
  
function err(str,rec) -- lazy to type outputChatBox each time 
    if not (rec) then rec = getRootElement() end 
    outputChatBox(str,rec,255,0,0) 
end 
  
function givemoney(thePlayer, command, amount) 
    if not (amount) then -- if the player didnot type the amount then 
    err("Incorrect syntax",thePlayer) 
    return 
    end 
    if not tonumber(amount) then -- if the amount is not a number then 
    err("Invalid amount",thePlayer) 
    return 
    end 
    if (tonumber(amount)>=getPlayerMoney(thePlayer)) then -- if the player's money is less than the amount then 
    err("Invalid amount",thePlayer) 
    return 
    end 
    if (tonumber(amount)==0) or (tonumber(amount)<=0) then -- if the amount is 0 or negative then 
    err("Invalid amount",thePlayer) 
    return 
    end 
    if isElement(gpickup[thePlayer]) then -- if the element gpickup[thePlayer] exists then 
    return 
    end 
    local x,y,z = getElementPosition(thePlayer) 
    local x = x + 5   
    gpickup[thePlayer] = createPickup(x,y,z,3,1212) 
    setElementData(gpickup[thePlayer], "moneyDropped", math.ceil(tonumber(amount))) 
    takePlayerMoney (thePlayer, math.abs(math.ceil(tonumber(amount)))) 
    addEventHandler('onPickupHit', gpickup[thePlayer], takemoney) 
end 
addCommandHandler ("givemoney", givemoney) 

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