Trying to apply a variable to a player

Hello,I’am trying to apply a variable to a player,Like:
function SWEP:Equip(ply)
The lua error I get is:Fuction arguments expected near =
I’am trying to do this so I get it on a derma screen later on.


ply.strength = 20

Oh,hah alright it worked but now when I try to call a function with it to print “Strength” it tells me tried to concatenate local “Whatsmystrength” (Name of the function) <a nil value>

Post your code

Can you show us your code?

function printMyResistance(whatsMyStrength)

PrintMessage( HUD_PRINTTALK,"Your Strength is “…whatsMyStrength…”!

function chatCommand( ply, text, public )
if (string.sub(text, 1, 4) == “/Strength”) then
printMyStrength( BUL_RES )


Is the function that shows the strength and the one where its supposed to tag the player with strength =20 is the one I first mentioned in my original post.

Your first fucntion seems okay. However the second function I am still trying to figure out how it could possibly work. string.sub(text,1,4) is 4 characters long. How can that equal to “/Strength”, if that has 9 characters :stuck_out_tongue:

Also, are you calling printMyResistance() with soem variable inside it? For example printMyResistance(“20”)

Oh yeah I switched out it was a 4 character word a few minutes ago,my bad,I just made it function printmyresistance(BUL_RES) (The variable I’am trying to get the value for.)

Now that I am looking at it, make sure that the strength variable is stored serverside for each player, since PrintMessage is a serverside function

We’re going to need the code for whatsMyStrength, assuming it’s a function.

How do I do that?

Sorry to sound like a douche, but if you dont know what clientside and serverside is and how stuff works there, there is no point in explaining it here. There are some nice tutorials on youtube for beginners, like here

Alright,thank you,I do know what they are though just not how to store on them,but you’re right I’am quite new to this.

to store the variable serverside, simply put it in a serverside file, like your addons init.lua, there you can set it serverside. To set it clientside, do it in a client file, eg. cl_init.lua.

You need to add () to the end of the function, like this:


Its a function argument, not a function

I would try to give my best response, but I can’t think logically after reading what you just said; I may sue you if this lasts for longer than 48 hours.
Functions are not used as arguments like that, but are in the likes of hooks which actually uses the function as an argument to be called when it has to be, not when hook.Add is called.
The function in PrintMessage is used the way I said it should be because it has to be called there to have it return the needed value. It is not an argument.

TL;DR: It’s treating it as a variable, which doesn’t exist, because it’s being used as a variable instead of a function.

You dont need to explain this to me. However, I NEVER seen him use whatsMyStrength as a function anywhere, not even defined the function in the code he posted, only as a parameter. I know what you are trying to explain me, realy I am. But I dont think he uses it as a function.

whatsMyStrength is the argument in his printMyResistance function. I may sue you if this lasts for longer than 48 hours.

function printMyStrength(whatsMyStrength)

    PrintMessage( HUD_PRINTTALK, "Your Strength is " .. whatsMyStrength .. "!
" )


-- make sure that chatCommand is actually run when the player chats!
-- you can use an OnPlayerChat hook to do this clientside if it's not already hooked, or PlayerSay on the serverside
function chatCommand( ply, text, public )

    if IsValid( ply ) and ( string.sub( text, 1, 9 ) == "/Strength" ) then
        -- pass the player's strength as an argument to the printMyStrength function
        printMyStrength( ply.Strength )

        return false



-- A helper function to set any player's strength
function SetMyStrength( ply, strength )

    if IsValid( ply ) then
        -- you wouldn't use a colon (:) here since you're not calling a function on the player metatable, you're just setting a value.
        -- this is why you got the "Function arguments expected near =" error before.
        ply.Strength = strength



function SWEP:Equip ( ply )
    SetMyStrength( ply, 20 )


Things I did:

  • Fixed some syntactical stuff (e.g. ply:Strength to ply.Strength, etc.)
  • Added a helper function (SetMyStrength)
  • Fixed the substring issue (bad length, you could also check out string.StartWith as a substitute for string.sub in this case

I have no idea how all of this is organized on your end, but hopefully this will help you with some of the issues. You would probably also benefit from checking whether the player actually has strength before you print their strength in the chat, since you’re only setting it when they equip the SWEP, but the command can apparently be run at any time.