Simple speed code

It runs without errors but when I type SetSpeed 150 500 it says UNKNOWN COMMAND


 if SERVER then
    function SetSpeed( ply, cmd, args )
        if ValidEntity( ply ) and ply:EntIndex( ) ~= 0 then
                GM:SetPlayerSpeed( ply, math.Clamp( args[ 1 ], 0, 500 ), math.Clamp( args[ 2 ], 0, 1000  ) )
                ply:ChatPrint("Run speed is now " .. args[ 1 ] .. ", and Walk speed is now" .. args[ 2 ] .. "." )
        end
    end
    
-- else
    -- concommand.Add( "openspeedmenu", function( ply, cmd, args )
        -- if ply:EntIndex( ) ~= 0 then
            -- local frame = vgui.Create( "DFrame" );
            -- frame:SetTall( 150 );
            -- frame:SetWide( 125 );
            -- frame:SetPos( ScrW( ) / 2 - frame:GetWide( ) / 2, ScrH( ) / 2 - frame:GetTall( ) / 2 );
            -- frame:SetTitle( "Speed Menu" );
            -- frame:SetDraggable( false );
            -- frame:ShowCloseButton( true );
            -- frame:MakePopup( );
            
            -- local walkspeed = vgui.Create( "DButton", frame );
            -- frame:SetTall( 50 );
            -- frame:SetWide( 250 );
        -- end;
    -- end );
end
hook.Add( "PlayerSpawn", "playerSetSpeed", SetSpeed() )
concommand.Add( "SetSpeed", SetSpeed() )


you are not using concommand.Add correctly
[lua]concommand.Add( “SetSpeed”, SetSpeed )[/lua] is what you want to use

Thanks, but now it’s saying


 
includes/extensions/math.lua:66: attempt to compare string with number


New code.


 if SERVER then
    function SetSpeed( ply, cmd, args )
        if ValidEntity( ply ) and ply:EntIndex( ) ~= 0 then
            GAMEMODE:SetPlayerSpeed( ply, math.Clamp( args[ 1 ], 0, 500 ), math.Clamp( args[ 2 ], 0, 1000  ) )
            ply:ChatPrint("Walk speed is now " .. args[ 1 ] .. ", and Run speed is now" .. args[ 2 ] .. "." )
        end
    end
    
-- else
    -- concommand.Add( "openspeedmenu", function( ply, cmd, args )
        -- if ply:EntIndex( ) ~= 0 then
            -- local frame = vgui.Create( "DFrame" );
            -- frame:SetTall( 150 );
            -- frame:SetWide( 125 );
            -- frame:SetPos( ScrW( ) / 2 - frame:GetWide( ) / 2, ScrH( ) / 2 - frame:GetTall( ) / 2 );
            -- frame:SetTitle( "Speed Menu" );
            -- frame:SetDraggable( false );
            -- frame:ShowCloseButton( true );
            -- frame:MakePopup( );
            
            -- local walkspeed = vgui.Create( "DButton", frame );
            -- frame:SetTall( 50 );
            -- frame:SetWide( 250 );
        -- end;
    -- end );
end
hook.Add( "PlayerSpawn", "playerSetSpeed", SetSpeed() )
concommand.Add( "SetSpeed", SetSpeed )


Ha, just realized I messed up the SetPlayerSpeed function with the concommand syntax. -FIXED

See this is not helping, I can’t learn when no one helps.

made it sound like you fixed your problem ._.

[editline]01:30AM[/editline]

Also, use the [ lua ][ /lua ] tags so we can see the lines better and what line is line 66

[editline]01:35AM[/editline]

ohh wait nvm…didn’t read the file that it said. Ok well I’m not 100% sure but I think
[lua] if ValidEntity( ply ) and ply:EntIndex( ) ~= 0 then [/lua]
is the wrong syntax, however I don’t know the correct way of doing that but i’ll look now

That is perfectly fine syntax.

[editline]10:30AM[/editline]

You need to tonumber the args of the concommand before you can use them in math.Clamp.

[lua]GAMEMODE:SetPlayerSpeed( ply, math.Clamp( tonumber(args[ 1 ]), 0, 500 ), math.Clamp( tonumber(args[ 2 ]), 0, 1000 ) )[/lua]

No no, I fixed “Ha, just realized I messed up the SetPlayerSpeed function with the concommand syntax. -FIXED” Not the whole code

[editline]04:58AM[/editline]

Thanks.

[lua] if SERVER then
function SetSpeed( ply, cmd, args )
if ValidEntity( ply ) and ply:EntIndex( ) ~= 0 then
if type( tonumber( args[ 1 ] ) ) == “number” then
GAMEMODE:SetPlayerSpeed( ply, math.Clamp( tonumber( args[ 1 ] ), 0, 500 ), math.Clamp( tonumber( args[ 2 ] ), 0, 1000 ) );
ply:ChatPrint("Run speed is now " … math.Clamp( tonumber( args[ 1 ], 0, 500 ) ) … ", and Walk speed is now " … math.Clamp( tonumber( args[ 2 ], 0, 1000 ) ) … “.”);
end;
end;
end;

– else
– concommand.Add( “openspeedmenu”, function( ply, cmd, args )
– if ply:EntIndex( ) ~= 0 then
– local frame = vgui.Create( “DFrame” );
– frame:SetTall( 150 );
– frame:SetWide( 125 );
– frame:SetPos( ScrW( ) / 2 - frame:GetWide( ) / 2, ScrH( ) / 2 - frame:GetTall( ) / 2 );
– frame:SetTitle( “Speed Menu” );
– frame:SetDraggable( false );
– frame:ShowCloseButton( true );
– frame:MakePopup( );

        -- local walkspeed = vgui.Create( "DButton", frame );
        -- frame:SetTall( 50 );
        -- frame:SetWide( 250 );
    -- end;
-- end );

end;
hook.Add( “PlayerSpawn”, “playerSetSpeed”, SetSpeed() );
concommand.Add( “SetSpeed”, SetSpeed );
[/lua]

This does work but two things, one, it don’t carry over if you die, and two, it gives me an error:

[lua]
:8: bad argument #2 to ‘tonumber’ (base out of range)
[/lua]

Nvm about the error, it seemed I had the ) in the wrong spot on the tonumber

You need to remove the brackets (parenthesis) on line 29 in that code (in the hook.Add), at the moment you are passing the return value of SetSpeed to the hook.

When I remove the () in the hook:

[lua]
ERROR: Hook ‘playerSetSpeed’ Failed: :6: attempt to index local ‘args’ (a nil value)
Removing Hook ‘playerSetSpeed’
[/lua]

Obviously when you hook to the function it isn’t going to pass the ply, cmd and args parameters to the callback like it would with the concommand.

So how would I fix this?

That is up to you, this is a design flaw and not a Lua question now.

Damn…

any way to store the args into a table and overwrite them when you use the concmd again?

[editline]06:34AM[/editline]

Is there such thing as a clientside table, if so, then that might work.

Why can’t you just create a function that the concommand function is a wrapper of?

Also, all the hooks are on the wiki. **[Gamemode.PlayerSpawn

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawn)** only provides you with the player entity.

You can store the speeds on the player table and overwrite them when they use the concommand.

[lua]player_object.RunSpeed = 100[/lua]

So, would I use this like:

[lua]
if CLIENT then
ply_object.WalkSpeed = args[ 1 ]
ply_object.RunSpeed = args[ 2 ]
end;
[/lua]

then check against the current speed and change it?

Something more like this:

[lua]if SERVER then
_R.Player.WalkSpeed = 100; – Change these defaults to whatever you like
_R.Player.RunSpeed = 300; – Default
function SetSpeed( ply, _, args )
if ValidEntity( ply ) then
ply.WalkSpeed = math.Clamp(tonumber(args[1]) or ply.WalkSpeed, 0, 500);
ply.RunSpeed = math.Clamp(tonumber(args[2]) or ply.RunSpeed, 0, 1000);
GAMEMODE:SetPlayerSpeed(ply, ply.WalkSpeed, ply.RunSpeed);
ply:ChatPrint("Run speed is now " … ply.RunSpeed … ", and Walk speed is now " … ply.WalkSpeed … “.”);
end
end
hook.Add(“PlayerSpawn”, “playerSetSpeed”, SetSpeed);
concommand.Add(“SetSpeed”, SetSpeed);
end
[/lua]

See, what does the . in Player.RunSpeed mean?

and what is with the _

_R.Player is the player metatable.

So, _R.Player.RunSpeed = 100 sets a variable in the player metatable.

Oh, so _R.Player is the table and RunSpeed is the index?