Jump to content

Need help with Afk script


Price.

Recommended Posts

so basically I was trying to make that if you do /afk you go to another dimension and when you press W ,S,D or A buttons, you get back to main dimension as you're back from afk, but idk got some errors when I put local player gives me error

Line 32: attempt to compare a number with boolean

line 31: Bad argument @ 'getElementDimension' [expected element at argument 1, got nil] and some errors when i do /afk

line 7: Bad argument @ 'isPedinVehicle' [Expected ped at argument 1, got string 'afk']

same error at line 2 'getPedOccupiedVehicle same @ line 10:'isPedonGround'

function afkCommand(thePlayer, cmd, r, g, b) 
 local theVehicle = getPedOccupiedVehicle ( thePlayer ) 
    local level = getPlayerWantedLevel ( thePlayer ) 
    if ( level > 0 ) then  
        outputChatBox("You cannot go AFK while being wanted!", thePlayer, 255, 255, 255) 
    else 
        if isPedInVehicle(thePlayer) then 
            outputChatBox("You must be on foot to use this command.", thePlayer, 255, 255, 255) 
        else 
            if isPedOnGround(thePlayer) then 
                if getElementDimension(thePlayer) < 10 then 
                    outputChatBox("You will be AFK in 5 seconds", thePlayer, 255, 255, 255) 
                    setTimer( 
                        function() 
                        setElementDimension(thePlayer, math.random(10, 1000)) 
                        setElementFrozen(thePlayer, true) 
                        end, 5000, 1 
                    ) 
                end 
            else 
                outputChatBox("You must be on the ground to use this command.", thePlayer, 255, 255, 255) 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, thePlayer, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(thePlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
                setElementDimension(thePlayer, 0) 
                setElementFrozen(thePlayer, false) 
                outputChatBox("You are no longer AFK", thePlayer, 255, 255, 255) 
            end 
        end 
    end 
end 
addEventHandler("onClientKey", root, returnafk) 

Link to comment
  
function afkCommand(thePlayer, cmd, r, g, b) 
 local theVehicle = getPedOccupiedVehicle ( thePlayer ) 
    local level = getPlayerWantedLevel ( thePlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", thePlayer, 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", thePlayer, 255, 255, 255) 
        else 
            if isPedOnGround(thePlayer) then 
                if getElementDimension(thePlayer) < 10 then 
                    outputChatBox("You will be AFK in 5 seconds", thePlayer, 255, 255, 255) 
                    setTimer( 
                        function() 
                        setElementDimension(thePlayer, math.random(10, 1000)) 
                        setElementFrozen(thePlayer, true) 
                        end, 5000, 1 
                    ) 
                end 
            else 
                outputChatBox("You must be on the ground to use this command.", thePlayer, 255, 255, 255) 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, thePlayer, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(thePlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
                setElementDimension(thePlayer, 0) 
                setElementFrozen(thePlayer, false) 
                outputChatBox("You are no longer AFK", thePlayer, 255, 255, 255) 
            end 
        end 
    end 
end 
addEventHandler("onClientKey", root, returnafk) 
  

Link to comment
  • Moderators

clientside addCommandHandler has a differed syntax:

Serverside:

player playerSource, string commandName, [string arg1, string arg2, ...] 

Clientside:

string commandName, [string arg1, string arg2, ...] 

:arrow: Use at clientside localPlayer instead. (DON'T DEFINE localPlayer INSIDE THE FUNCTION)

Edited by Guest
Link to comment

line 3: bad argument @'getPedOccupiedVehicle' [Expected ped at argument 1, got string 'afk']

line 11: Bad argument @'isPedonGround [Expected ped at argument 1, got string 'afk']

line 22: bad argument @'outputchatbox' [Expected bool at argument 5, got number '255']

I've tried using localplayer and thePlayer, both same result

Link to comment

tried removing all definitions from function and got this 1 only error

line 13: bad argument @'outputChatbox"[Expected number at argument 2, got player

  
function afkCommand() 
 local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
    local level = getPlayerWantedLevel ( localPlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", localPlayer, 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", localPlayer, 255, 255, 255) 
        else 
            if isPedOnGround(localPlayer) then 
                if getElementDimension(localPlayer) < 10 then 
                    outputChatBox("You will be AFK in 5 seconds", localPlayer, 255, 255, 255) 
                    setTimer( 
                        function() 
                        setElementDimension(localPlayer, math.random(10, 1000)) 
                        setElementFrozen(localPlayer, true) 
                        end, 5000, 1 
                    ) 
                end 
            else 
                outputChatBox("You must be on the ground to use this command.", localPlayer, 255, 255, 255) 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(localPlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
                setElementDimension(localPlayer, 0) 
                setElementFrozen(localPlayer, false) 
                outputChatBox("You are no longer AFK", localPlayer, 255, 255, 255) 
            end 
        end 
    end 
end 
addEventHandler("onClientKey", root, returnafk) 
  

Link to comment
  • Moderators

See syntax of clientside outputChatBox and serverside outputChatBox, then you know what you are doing wrong.

Server

bool outputChatBox ( string text [, element visibleTo=getRootElement(), int r=231, int g=217, int b=176, bool colorCoded=false ] ) 

Client

bool outputChatBox ( string text [, int r=231, int g=217, int b=176, bool colorCoded=false ] ) 

Clientside doesn't need a target to send it too, since it happens on the client his pc.

Link to comment
  • Moderators

Try this:

Serverside

addEvent("afkStatus",true) 
addEventHandler("afkStatus",root, 
function (status) 
if status then 
    setPlayerName (client,getPlayerName(client).. "|AFK") 
else 
local name = string.gsub(getPlayerName(client),"|AFK", "") 
  
if name then 
    setPlayerName (client,name) 
end 
  
end 
end) 

Set afk

triggerServerEvent("afkStatus",localPlayer,true) 

Set not afk.

triggerServerEvent("afkStatus",localPlayer,false) 

Link to comment

nothing in debug and my name doesnt change

Client

  
function afkCommand() 
 local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
    local level = getPlayerWantedLevel ( localPlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", 255, 255, 255) 
        else 
            if isPedOnGround(localPlayer) then 
                if getElementDimension(localPlayer) < 10 then 
                triggerServerEvent("afkStatus",localPlayer,true) 
                    outputChatBox("You will be AFK in 5 seconds" , 255, 255, 255) 
                     setTimer( 
                        function() 
                        setElementDimension(localPlayer, math.random(10, 1000)) 
                        setElementFrozen(localPlayer, true) 
                        end, 5000, 1 
                    ) 
            else 
                outputChatBox("You must be on the ground to use this command.", 255,255,255) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(localPlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
            triggerServerEvent("afkStatus",localPlayer,false) 
                setElementDimension(localPlayer, 0) 
                setElementFrozen(localPlayer, false) 
                outputChatBox("You are no longer AFK", 255, 255, 255) 
                 
            end 
        end 
    end 
end 
addEventHandler("onClientKey", root, returnafk) 
  

server

addEvent("afkStatus",true) 
addEventHandler("afkStatus",root, 
function (status) 
if status then 
    setPlayerName (client,getPlayerName(client).. "|AFK") 
else 
local name = string.gsub(getPlayerName(client),"|AFK", "") 
  
if name then 
    setPlayerName (client,name) 
end 
  
end 
end) 

Link to comment

still nothing, should it be added like that?

Client:

function afkCommand() 
 ouputDebugString("write here which part of the code get executed") 
 local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
    local level = getPlayerWantedLevel ( localPlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", 255, 255, 255) 
        else 
            if isPedOnGround(localPlayer) then 
                if getElementDimension(localPlayer) < 10 then 
                triggerServerEvent("afkStatus",localPlayer,true) 
                    outputChatBox("You will be AFK in 5 seconds" , 255, 255, 255) 
                     setTimer( 
                        function() 
                        setElementDimension(localPlayer, math.random(10, 1000)) 
                        setElementFrozen(localPlayer, true) 
                        end, 5000, 1 
                    ) 
            else 
                outputChatBox("You must be on the ground to use this command.", 255,255,255) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(localPlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
            triggerServerEvent("afkStatus",localPlayer,false) 
                setElementDimension(localPlayer, 0) 
                setElementFrozen(localPlayer, false) 
                outputChatBox("You are no longer AFK", 255, 255, 255) 
                 
            end 
        end 
    end 
end 
  
addEventHandler("onClientKey", root, returnafk) 
  

Link to comment

same nothing appears

client:

function afkCommand() 
outputDebugString 
 local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
    local level = getPlayerWantedLevel ( localPlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", 255, 255, 255) 
        else 
            if isPedOnGround(localPlayer) then 
                if getElementDimension(localPlayer) < 10 then 
                outputDebugString 
                triggerServerEvent("afkStatus",localPlayer,true) 
                outputDebugString 
                    outputChatBox("You will be AFK in 5 seconds" , 255, 255, 255) 
                     setTimer( 
                        function() 
                        setElementDimension(localPlayer, math.random(10, 1000)) 
                        setElementFrozen(localPlayer, true) 
                        end, 5000, 1 
                    ) 
            else 
                outputChatBox("You must be on the ground to use this command.", 255,255,255) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(localPlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
            outputDebugString 
            triggerServerEvent("afkStatus",localPlayer,false) 
            outputDebugString 
                setElementDimension(localPlayer, 0) 
                setElementFrozen(localPlayer, false) 
                outputChatBox("You are no longer AFK", 255, 255, 255) 
                 
            end 
        end 
    end 
end 
  
addEventHandler("onClientKey", root, returnafk) 
  

server

outputDebugString 
addEvent("afkStatus",true) 
addEventHandler("afkStatus",root, 
function (status) 
if status then 
    setPlayerName (client,getPlayerName(client).. "|AFK") 
else 
local name = string.gsub(getPlayerName(client),"|AFK", "") 
 outputDebugString 
if name then 
    setPlayerName (client,name) 
end 
  
end 
end) 

PS: the afk is related to chat, like when you say any word having any of these letters"A,W,S or D" you get back from AFK

Link to comment
  • Moderators

You have to use outputDebugString the same as outputChatBox, does nothing else then outputChatBox in to your debug.

These debug logs are meant for you, so you can see what is going on.

addEvent("afkStatus",true) 
addEventHandler("afkStatus",root, 
function (status) 
outputDebugString("serverside part executed") 
if status then 
    setPlayerName (client,getPlayerName(client).. "|AFK") 
outputDebugString("set to afk") 
else 
local name = string.gsub(getPlayerName(client),"|AFK", "") 
if name then 
 outputDebugString("set to not afk") 
    setPlayerName (client,name) 
end 
  
end 
end) 

Sorry I do not have notepad ++ on my mac...... so no tabs.

Link to comment

same nothing

client

function afkCommand() 
outputDebugString("set to not afk") 
 local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
    local level = getPlayerWantedLevel ( localPlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", 255, 255, 255) 
        else 
            if isPedOnGround(localPlayer) then 
                if getElementDimension(localPlayer) < 10 then 
                outputDebugString("set to not afk") 
                triggerServerEvent("afkStatus",localPlayer,true) 
                outputDebugString("set to not afk") 
                    outputChatBox("You will be AFK in 5 seconds" , 255, 255, 255) 
                     setTimer( 
                        function() 
                        setElementDimension(localPlayer, math.random(10, 1000)) 
                        setElementFrozen(localPlayer, true) 
                        end, 5000, 1 
                    ) 
            else 
                outputChatBox("You must be on the ground to use this command.", 255,255,255) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(localPlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
            outputDebugString("set to not afk") 
            triggerServerEvent("afkStatus",localPlayer,false) 
            outputDebugString("set to not afk") 
                setElementDimension(localPlayer, 0) 
                setElementFrozen(localPlayer, false) 
                outputChatBox("You are no longer AFK", 255, 255, 255) 
                 
            end 
        end 
    end 
end 
  
addEventHandler("onClientKey", root, returnafk) 
  

server

addEvent("afkStatus",true) 
addEventHandler("afkStatus",root, 
function (status) 
outputDebugString("serverside part executed") 
if status then 
    setPlayerName (client,getPlayerName(client).. "|AFK") 
outputDebugString("set to afk") 
else 
local name = string.gsub(getPlayerName(client),"|AFK", "") 
if name then 
 outputDebugString("set to not afk") 
    setPlayerName (client,name) 
end 
  
end 
end) 

Link to comment
  • Moderators

What do you mean with nothing?

You are the one who needs to debugging it, show me the debug results.

and it is stupid to use the same debug text all over the script, you need to write what happens there.

Not just copy my text, so you don't know what part of the code get executed.

Don't forget the /debugscript 3

Link to comment

k changed the texts only, it doesnt matter as long as it doesn't show anything in char nor /debugscript 3 not even in server console

Client

function afkCommand() 
outputDebugString("testing this section") 
 local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
    local level = getPlayerWantedLevel ( localPlayer ) 
    if ( level > 0 ) then 
        outputChatBox("You cannot go AFK while being wanted!", 255, 255, 255) 
    else 
        if theVehicle then 
            outputChatBox("You must be on foot to use this command.", 255, 255, 255) 
        else 
            if isPedOnGround(localPlayer) then 
                if getElementDimension(localPlayer) < 10 then 
                outputDebugString("trying to figure it out") 
                triggerServerEvent("afkStatus",localPlayer,true) 
                outputDebugString("just another test") 
                    outputChatBox("You will be AFK in 5 seconds" , 255, 255, 255) 
                     setTimer( 
                        function() 
                        setElementDimension(localPlayer, math.random(10, 1000)) 
                        setElementFrozen(localPlayer, true) 
                        end, 5000, 1 
                    ) 
            else 
                outputChatBox("You must be on the ground to use this command.", 255,255,255) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("afk", afkCommand) 
  
function returnafk (button, press, r, g, b ) 
    if (press) then 
        if button == "w" or button == "s" or button == "d" or button == "a" then 
            local dimPlr = getElementDimension(localPlayer) 
            if  ((dimPlr >= 10) and (dimPlr <= 1000)) then 
            outputDebugString("random text") 
            triggerServerEvent("afkStatus",localPlayer,false) 
            outputDebugString("another test") 
                setElementDimension(localPlayer, 0) 
                setElementFrozen(localPlayer, false) 
                outputChatBox("You are no longer AFK", 255, 255, 255) 
                 
            end 
        end 
    end 
end 
  
addEventHandler("onClientKey", root, returnafk) 
  

addEvent("afkStatus",true) 
addEventHandler("afkStatus",root, 
function (status) 
outputDebugString("serverside part executed") 
if status then 
    setPlayerName (client,getPlayerName(client).. "|AFK") 
outputDebugString("set to afk") 
else 
local name = string.gsub(getPlayerName(client),"|AFK", "") 
if name then 
 outputDebugString("set to not afk") 
    setPlayerName (client,name) 
end 
  
end 
end) 

Link to comment
What does not work?

sorry for double reply but uhm as I said I wanted to do as when i do /afk a text |AFK gets added BESIDES his name not to change his entire name to AFK but to put |AFK besides his name, and actually Nothing really happens with the code so we are trying to debug the script manually still nothing happens.

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