How do I change a player variable with a custom command?

Dodobee

Avid Among us enjoyer
I need the player to be able to choose the amount of a custom player. variable using a command but I'm not sure how to do that.
 
Last edited:
You need to create a custom function that sets the field first, then connect it to a console command using COM_AddCommand.

Lua:
local function ChangeValue(p, field)
    --all the fields are strings in a command
    --need to use tonumber() if you're trying to set a numerical value
    if (field ~= nil) then
        p.customvalue = tonumber(field)
    end
end

--"commandname" can be any name you want
COM_AddCommand("commandname", ChangeValue)

The custom function can have as many arguments as you want. If you want a console command with 3 arguments, then you define the function like:
local function ChangeValue(p, field, otherfield, otherotherfield)

"p" is the player that runs the command. You can add the necessary validity checks yourself.

https://wiki.srb2.org/wiki/Lua/Functions#Console_library This page goes over that command, alongside some others, including setting a general console variable that's shared between all players.
 
I know it's been a while but I don't really understand how to do it. I want the player to be able to choose how long a timer lasts.
 
What I said should still work, though? Once the player runs commandname 30, for example, their p.customvaluefield will be 30. So if you wrote something like p.timer = p.customvalue, their timer will be set to 30, so you can decrease it freely from there.
 
Oh alright now I understand, thanks so much!
Post automatically merged:

Nevermind, it's not working, here's the code if it helps

Airwalking:
addHook("PlayerThink", function(player)
    if player.shouldairwalk == nil then
        player.shouldairwalk = false
    end
    
    if player.airwalklength == nil then
        player.airwalklength = 1*TICRATE
    end
    
    if player.airwalk == nil then
        player.airwalk = 0
    end
    
    if player.airwalked == nil then
        player.airwalked = false
    end
    
    if player.willtakedamage == nil then
        player.willtakedamage = false
    end
    
    if player.shouldntairwalk == nil then
        player.shouldntairwalk = false
    end
    
    if player.falling == nil then
        player.falling = false
    end
    
    if player.secondchance == nil then
        player.secondchance = 0*TICRATE
    end
    
    if player.stopaction == nil then
        player.stopaction = false
    end
    
    if (player.mo.eflags & MFE_UNDERWATER)
    or (player.mo.eflags & MFE_SPRUNG)
    or player.mo.state == S_PLAY_ROLL
    or player.mo.state == S_PLAY_PAIN
    or (player.pflags & PF_THOKKED)
    or (player.playerstate == PST_DEAD) then
    player.falling = false
    player.willtakedamage = false
    player.shouldntairwalk = true
    end
    
    if (P_IsObjectOnGround(player.mo) == false)
    and player.shouldairwalk == true
    and not (player.pflags & PF_JUMPED) then
    player.airwalk = player.airwalklength
    player.shouldairwalk = false
    end
    
    if (P_IsObjectOnGround(player.mo) == true) then
    player.shouldairwalk = true
    player.airwalk = 0*TICRATE
    player.airwalked = false
    player.shouldntairwalk = false
    player.falling = false
    player.secondchance = 0*TICRATE
    player.stopaction = false
    end
    
    if (P_IsObjectOnGround(player.mo) == false)
    and player.airwalk > -1 then
    player.airwalk = $ - 1
    end
    
    if not P_IsObjectOnGround(player.mo)
    and player.secondchance > -1 then
    player.secondchance = $ - 1
    end
    
    if player.airwalk > 0
    and player.mo.z/FRACUNIT - player.mo.floorz/FRACUNIT > 128
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.mo.momz < 1
    and player.shouldntairwalk == false then
    player.airwalked = true
    player.mo.state = S_PLAY_RUN
    player.mo.momz = 0
    end
    
    if player.airwalk > 0
    and player.stopaction == false
    and player.mo.z/FRACUNIT - player.mo.floorz/FRACUNIT < 128
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.airwalked == false
    and (P_IsObjectOnGround(player.mo) == false) then
    player.secondchance = 2
    player.stopaction = true
    end
    
    if player.secondchance > 0
    and player.mo.z/FRACUNIT - player.mo.floorz/FRACUNIT > 128
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.shouldntairwalk == false then
    player.airwalk = player.airwalklength
    end
    
    if player.airwalk < 0
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.shouldntairwalk == false
    and player.mo.z/FRACUNIT - player.mo.floorz/FRACUNIT > 128
    and player.falling == false then
    player.mo.momz = 10*FRACUNIT
    player.falling = true
    player.mo.state = S_PLAY_DEAD
    S_StartSound(player.mo, sfx_s3k51)
    end
    
    if player.airwalk > 0
    and player.airwalk <= 7
    and (player.cmd.buttons & BT_JUMP)
    and player.shouldntairwalk == false then
    player.shouldntairwalk = true
    player.mo.momz = 10*FRACUNIT
    player.pflags = $|PF_JUMPED|PF_THOKKED
    player.mo.state = S_PLAY_JUMP
    S_StartSound(player.mo, sfx_jump)
    end
    
    if player.falling == true
    and player.mo.momz > -25*FRACUNIT then
    player.mo.momz = $ - 1*FRACUNIT/2
    end
    
    if player.falling == true
    and player.mo.momz < -25*FRACUNIT then
    player.willtakedamage = true
    end
    
    if player.willtakedamage == true
    and player.mo.momz > 0 then
    player.willtakedamage = false
    player.falling = false
    player.shouldntairwalk = true
    end
    
    if player.willtakedamage == true
    and (P_IsObjectOnGround(player.mo) == true) then
    P_DamageMobj(player.mo)
    S_StartSound(player.mo, sfx_doord2)
    player.willtakedamage = false
    player.mo.momz = -1*FRACUNIT
    end
end)

And here is what I did for the command

Command for setting the airwalk duration:
local function ChangeValue(player, airwalk)
    --all the fields are strings in a command
    --need to use tonumber() if you're trying to set a numerical value
    if (field ~= nil) then
        player.airwalklength = tonumber(field)*TICRATE
    end
end

--"commandname" can be any name you want
COM_AddCommand("airwalk", ChangeValue, COM_ADMIN)
 
Last edited:
and player.mo.z/FRACUNIT - player.mo.floorz/FRACUNIT > 128

Don't do that. Do;

and player.mo.z - player.mo.floorz > 128*FRACUNIT

Tested it on my end, works fine after this. Of course, 128 FRACUNITs is pretty high so most of GFZ1 won't work for it. Additionally, the run animation gets stuck if you aren't running fast enough since the game tries to force the character back into walking frames, so only activating it when you're going fast might be needed.

My adjustment to the code, just in case

code:
local function airwalkSetup(player)
    player.shouldairwalk = false
    player.airwalklength = 1*TICRATE
    player.airwalk = 0
    player.airwalked = false
    player.willtakedamage = false
    player.shouldntairwalk = false
    player.falling = false
    player.secondchance = 0*TICRATE
    player.stopaction = false

    player.airwalksetup = true
end

addHook("PlayerSpawn", airwalkSetup)

addHook("PlayerThink", function(player)
    if not player.airwalksetup then
        airwalkSetup(player)
    end
   
    if (player.mo.eflags & MFE_UNDERWATER)
    or (player.mo.eflags & MFE_SPRUNG)
    or player.mo.state == S_PLAY_ROLL
    or player.mo.state == S_PLAY_PAIN
    or (player.pflags & PF_THOKKED)
    or (player.playerstate == PST_DEAD) then
        player.falling = false
        player.willtakedamage = false
        player.shouldntairwalk = true
    end
   
    if (P_IsObjectOnGround(player.mo) == false)
    and player.shouldairwalk == true
    and not (player.pflags & PF_JUMPED) then
        player.airwalk = player.airwalklength
        player.shouldairwalk = false
    end
   
    if (P_IsObjectOnGround(player.mo) == true) then
        player.shouldairwalk = true
        player.airwalk = 0*TICRATE
        player.airwalked = false
        player.shouldntairwalk = false
        player.falling = false
        player.secondchance = 0*TICRATE
        player.stopaction = false
    end
   
    if (P_IsObjectOnGround(player.mo) == false)
    and player.airwalk > -1 then
        player.airwalk = $ - 1
    end
   
    if not P_IsObjectOnGround(player.mo)
    and player.secondchance > -1 then
        player.secondchance = $ - 1
    end
   
    if player.airwalk > 0
    and player.mo.z - player.mo.floorz > 128*FRACUNIT
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.mo.momz < 0
    and player.shouldntairwalk == false then
        player.airwalked = true
        player.mo.state = S_PLAY_RUN
        player.mo.momz = 0
    end
   
    if player.airwalk > 0
    and player.stopaction == false
    and player.mo.z - player.mo.floorz < 128*FRACUNIT
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.airwalked == false
    and (P_IsObjectOnGround(player.mo) == false) then
        player.secondchance = 2
        player.stopaction = true
    end
   
    if player.secondchance > 0
    and player.mo.z - player.mo.floorz > 128*FRACUNIT
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.shouldntairwalk == false then
        player.airwalk = player.airwalklength
    end
   
    if player.airwalk < 0
    and not (player.pflags & PF_JUMPED)
    and not (player.pflags & PF_THOKKED)
    and player.shouldntairwalk == false
    and player.mo.z - player.mo.floorz > 128*FRACUNIT
    and player.falling == false then
        player.mo.momz = 10*FRACUNIT
        player.falling = true
        player.mo.state = S_PLAY_DEAD
        S_StartSound(player.mo, sfx_s3k51)
    end
   
    if player.airwalk > 0
    and player.airwalk <= 7
    and (player.cmd.buttons & BT_JUMP)
    and player.shouldntairwalk == false then
        player.shouldntairwalk = true
        player.mo.momz = 10*FRACUNIT
        player.pflags = $|PF_JUMPED|PF_THOKKED
        player.mo.state = S_PLAY_JUMP
        S_StartSound(player.mo, sfx_jump)
    end
   
    if player.falling == true
    and player.mo.momz > -25*FRACUNIT then
        player.mo.momz = $ - 1*FRACUNIT/2
    end
   
    if player.falling == true
    and player.mo.momz < -25*FRACUNIT then
        player.willtakedamage = true
    end
   
    if player.willtakedamage == true
    and player.mo.momz > 0 then
        player.willtakedamage = false
        player.falling = false
        player.shouldntairwalk = true
    end
   
    if player.willtakedamage == true
    and (P_IsObjectOnGround(player.mo) == true) then
        P_DamageMobj(player.mo)
        S_StartSound(player.mo, sfx_doord2)
        player.willtakedamage = false
        player.mo.momz = -1*FRACUNIT
    end
end)

local function ChangeValue(player, field)
    --all the fields are strings in a command
    --need to use tonumber() if you're trying to set a numerical value
    if (field ~= nil) then
        player.airwalklength = tonumber(field)*TICRATE
    end
end

--"commandname" can be any name you want
COM_AddCommand("airwalk", ChangeValue, COM_ADMIN)
 
Last edited:
For some reason, player.airwalklength stays as 35 even after the command.
(I apologize for bugging you so much)
 
local function ChangeValue(player, airwalk) should've been local function ChangeValue(player, field), dunno why you changed it.
 

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top