Jump to content

Store robbery chatbox problem


-Bane-

Recommended Posts

In the chat the message reports "nil" instead of the amound of money stolen.

client:

-- Player aims at another player or objcet 
cooldown = {} 
function targetingActivated ( target ) 
    -- Check so the team is criminals, that the criminal  
    -- is aiming and that the location is interior 
    local theTeam = getPlayerTeam ( localPlayer ) 
    if not isTimer(cooldown[localPlayer] ) and getControlState("aim_weapon") and  
        getElementInterior( localPlayer ) > 0 and isElement( target ) and  
        ( getPlayerTeam( localPlayer ) == getTeamFromName( "Criminals" ) or 
        getPlayerTeam( localPlayer ) == getTeamFromName( "Civilians" ) or 
        getPlayerTeam( localPlayer ) == getTeamFromName( "Gangsters" ) or  
        getPlayerTeam( localPlayer ) == getTeamFromName( "Unemployed" )) then  
        -- Stores may only be robbed every 5 minutes 
        outputChatBox( "You have robbed this store, stay inside until you get the cash!", 255, 0, 0 ) 
        triggerServerEvent( "onRob", localPlayer, target )  
        cooldown[localPlayer] = setTimer(function() end, 200000, 1 ) 
    end 
end 
addEventHandler ( "onClientPlayerTarget", getRootElement(), targetingActivated ) 
  
function showTimeLeft( ) 
    if getElementData( localPlayer, "rob" ) then 
        local endTime = tonumber( getElementData( localPlayer, "robTime" )) 
        local currentTime = tonumber( getElementData( localPlayer, "robTime2" )) 
        local sx, sy = guiGetScreenSize( ) 
        dxDrawText ( "Time left: "..tostring(math.floor((endTime-currentTime)/1000)), sx-300, sy-50, 0, 0,  
            tocolor( 0, 255, 0, 255 ), 1.5, "default-bold" ) 
    end 
end 
addEventHandler( "onClientRender", root, showTimeLeft ) 

server:

ped = { } 
peds = { 
    --x, y, z, dimension, interior, rotation, skinID 
    [1]={ 161, -81, 1001.8046875, 3, 18, 180, 93 }, 
    [2]={ 161, -81, 1001.8046875, 2, 18, 180, 93 }, 
    [3]={ 161, -81, 1001.8046875, 1, 18, 180, 93 }, 
    [4]={ 161, -81, 1001.8046875, 0, 18, 180, 93 }, 
     
    [5]={ 204.7978515625, -7.896484375, 1001.2109375, 2, 5, 270, 93 }, 
    [6]={ 204.7978515625, -7.896484375, 1001.2109375, 1, 5, 270, 93 }, 
    [7]={ 204.7978515625, -7.896484375, 1001.2109375, 0, 5, 270, 93 }, 
     
    [8]={ 203.4, -41.7, 1001.8046875, 2, 1, 180, 93 }, 
    [9]={ 203.4, -41.7, 1001.8046875, 1, 1, 180, 93 }, 
    [10]={ 203.4, -41.7, 1001.8046875, 0, 1, 180, 93 }, 
     
    [11]={ 204.2080078125, -157.8193359375, 1000.5234375, 0, 14, 180, 93 }, 
     
    [12]={ 206.3759765625, -127.5380859375, 1003.5078125, 1, 3, 180, 93 }, 
    [13]={ 206.3759765625, -127.5380859375, 1003.5078125, 0, 3, 180, 93 }, 
     
    [14]={ 206.3349609375, -98.703125, 1005.2578125, 3, 15, 180, 93 }, 
    [15]={ 206.3349609375, -98.703125, 1005.2578125, 2, 15, 180, 93 }, 
    [16]={ 206.3349609375, -98.703125, 1005.2578125, 1, 15, 180, 93 }, 
    [17]={ 206.3349609375, -98.703125, 1005.2578125, 0, 15, 180, 93 }, 
} 
  
cancelTimers = {} 
function loadPeds() 
    for k=1, #peds do 
        -- Create the ped 
        ped[k] = createPed ( peds[k][7], peds[k][1], peds[k][2], peds[k][3] ) 
        setElementDimension( ped[k], peds[k][4] ) 
        setElementInterior( ped[k], peds[k][5] ) 
        setPedRotation ( ped[k], peds[k][6] ) 
        setTimer( setElementHealth, 100, 0, ped[k], 100 ) 
    end 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), loadPeds) 
  
-- Robbery time 
function CounterTime( crim ) 
    if isElement( crim ) then 
        local time = getTickCount( ) 
        setElementData( crim, "robTime2", time ) 
    end 
end 
  
-- Define law  
lawTeams = { 
    [getTeamFromName("Police")] = true,  
    [getTeamFromName("SAPD")] = true,  
    [getTeamFromName("SWAT")] = true,  
    [getTeamFromName("FBI")] = true,  
    [getTeamFromName("Army")] = true,  
    [getTeamFromName("Emergency service")] = true  
} 
-- Antispam timer 
robTimer = { } 
  
-- Do the rob 
function robStore( target ) 
    if getElementType( target ) == "ped" and not isTimer(robTimer[target]) then 
        -- Robbery in progress 
        setElementData( client, "rob", true ) 
        local money = math.random( 500, 30000 ) 
        local robtime = math.random(120000, 240000) 
         
        -- Allow count down timer 
        setElementData( client, "robTime", robtime+getTickCount( )) 
        setElementData( client, "robTime2", getTickCount( )) 
        setTimer( CounterTime, 1000, (math.floor(robtime)/1000), client ) 
         
        -- When the robbery is finished 
        setTimer( payForRob, robtime, 1, client, money ) 
        setTimer( robStatus, robtime, 1, client, target ) 
        cancelTimers[client] = setTimer( cancelRob, (math.floor(robtime)/100), 100, client, target ) 
         
        -- Set the wanted level 
        if getPlayerWantedLevel( client ) <= 3 then 
            setPlayerWantedLevel( client, getPlayerWantedLevel( client ) + 3 ) 
        else 
            setPlayerWantedLevel( client, 6 ) 
        end 
        setPedAnimation( target, "shop", "shp_rob_givecash", -1, false ) 
        outputChatBox( "You committed the crime of robbery ("..round(money*0.0001,2).." stars)", client, 255, 255, 0 ) 
         
        -- Send alarm call to all the cops 
        local cops = getElementsByType( "player" )  
        for theKey,cop in ipairs(cops) do  
            if lawTeams[getPlayerTeam(cop)] then 
                cx,cy,cz = getElementPosition( client ) 
                outputChatBox( "#0000BB(911): #EEEEEERobbery in progress at: "..getZoneName( cx, cy, cz ), cop, 255, 255, 255, true ) 
                outputChatBox( "Robbery in progress at: "..getZoneName( cx, cy, cz ), cop, 255, 0, 0 ) 
            end 
        end 
         
        -- Set Timer 
        robTimer[target] = setTimer(function() end, 10800000, 1 ) 
    elseif isTimer(robTimer[target]) then 
        outputChatBox( "Get the hell out of here, this store was just robbed!", client, 255, 0, 0 ) 
    end 
end 
addEvent( "onRob", true ) 
addEventHandler( "onRob", root, robStore ) 
  
function payForRob( crim, amount ) 
    if isElement( crim ) then 
        if getElementData( crim, "rob" ) then  
            givePlayerMoney( crim, amount )  
            outputChatBox( "Robbery sucsessfull, you earned: "..tostring(money), crim, 0, 150, 0 )  
        end  
    end 
end 
  
function robStatus( crim, target, money )  
    if isElement( crim ) then 
        if getElementData( crim, "rob" ) then  
            outputChatBox( "Rob successfully, now escape before the cops arrive!", crim, 0, 255, 0 )             
        end 
        setPedAnimation( target, nil, nil ) 
        setElementData( crim, "rob", false ) 
    end  
end 
  
-- Check if the rob should be interrupted 
function cancelRob( crim, target ) 
    if isElement( crim ) then 
        if getElementInterior( crim ) == 0 or getElementData( crim, "arrested" ) or getElementData( crim, "Jailed" ) == "Yes" then 
            setElementData( crim, "rob", false ) 
            outputChatBox( "Robbery failed because you left the store!", crim, 255, 0, 0) 
            setPedAnimation( target, nil, nil ) 
            if isTimer( cancelTimers[crim] ) then 
                killTimer( cancelTimers[crim] ) 
            end 
        end 
    end 
end 
  
-- Round float values 
function round(number, digits) 
    local mult = 10^(digits or 0) 
    return math.floor(number * mult + 0.5) / mult 
end 

Link to comment

That's origionally the AC RP robbery script which was made using the exported function outputTopBar from "ac-message" which has the same syntax as outputChatBox, however due to complaints from lazy players who didn't wanted to download and use this: https://community.multitheftauto.com/index.php?p=resources&s=details&id=8620, I had to write a special version for mta community, however since this bug was a part of the current script it ended up in the final version, it will be upgraded soon due to the amount of dowanloads.

Solution:

Line 111, server, change from this:

outputChatBox( "Robbery sucsessfull, you earned: "..tostring(money), crim, 0, 150, 0 )  

to this:

outputChatBox( "Robbery sucsessfull, you earned: "..tostring(amount), crim, 0, 150, 0 )  

Do not remove the local on line 66, server since that will give all players the same amount during any robbery and change it while a store is robbed in case another player is robbing. The variable should be local and passed as an argument to the function payForRob which is used to verify that the players actually stayed in the store until the time expired and avoid getting arrested or killed.

Link to comment
  • Moderators

@Anubhav and @Mr.Pres[T]ege: Never use global variables to store datas that belong to a specific player just because you need to access this data in another function on the server side. This is a really bad practice you are sharing with other scripters. Doing that will just introduce bugs in the system as mrbrutus1467 explained in his last paragraph.

The real solution is to put the local keyword back and use the variable sent to that function:

local money = math.random( 500, 30000 ) 

and

outputChatBox( "Robbery sucsessfull, you earned: "..tostring(amount), crim, 0, 150, 0 ) 

Link to comment
That's origionally the AC RP robbery script which was made using the exported function outputTopBar from "ac-message" which has the same syntax as outputChatBox, however due to complaints from lazy players who didn't wanted to download and use this: https://community.multitheftauto.com/index.php?p=resources&s=details&id=8620, I had to write a special version for mta community, however since this bug was a part of the current script it ended up in the final version, it will be upgraded soon due to the amount of dowanloads.

Solution:

Line 111, server, change from this:

outputChatBox( "Robbery sucsessfull, you earned: "..tostring(money), crim, 0, 150, 0 )  

to this:

outputChatBox( "Robbery sucsessfull, you earned: "..tostring(amount), crim, 0, 150, 0 )  

Do not remove the local on line 66, server since that will give all players the same amount during any robbery and change it while a store is robbed in case another player is robbing. The variable should be local and passed as an argument to the function payForRob which is used to verify that the players actually stayed in the store until the time expired and avoid getting arrested or killed.

@Anubhav and @Mr.Pres[T]ege: Never use global variables to store datas that belong to a specific player just because you need to access this data in another function on the server side. This is a really bad practice you are sharing with other scripters. Doing that will just introduce bugs in the system as mrbrutus1467 explained in his last paragraph.

The real solution is to put the local keyword back and use the variable sent to that function:

local money = math.random( 500, 30000 ) 

and

outputChatBox( "Robbery sucsessfull, you earned: "..tostring(amount), crim, 0, 150, 0 ) 

Thanks guys!

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