mapping lua_run player speed

Create a map of Garry’s mod. Is it possible to use the entity lua_run, that would change the speed of the player?
For yet know nothing about lua :frowning: I am wrote in lua_run player: SetWalkSpeed (40)
When activated entity lua_run, in error console

[ERROR] RunString: 1: attempt to call method ‘SetWalkSpeed’ (a nil value)

  1. unknown - RunString: 1
    2. RunString - [C]: - 1
    3. RunCode - gamemodes / base / entities / entities / lua_run.lua: 41
    4. unknown - gamemodes / base / entities / entities / lua_run.lua: 49

Can anyone suggest or help? Thank you very much in advance!

P.S. Sorry for my english :slight_smile:

You have to select a player. If you want it to apply to all players, then you can do:


local tPlayers = player.GetAll()

for i = 1, #tPlayers do
   tPlayers*:SetWalkSpeed(40)
end

it’s probably not a good idea to embed that into your map.

What is it being used for?

You should not be using Lua for this, you should be using the stuff that CS:S maps use.

Thank you!

If you use the entity player_speedmod, which is already available in hammer’e. If a player uses the shift key
he can run faster, and you can not change it in any way :frowning:

Little OT:

I’m curious as to why you wrote your loop like that. Is something like that more efficient than this?



for k, v in pairs(player.GetAll()) do
 -- do something to the player
end


is used to count the numerically & sequentially indexed part of a table (some refer to it as the array side)

pairs (shorthand for next, <table>, nil) takes into account the numeric indices as well as all of the table’s key/value pairs (both the array and hash “sides”).

Since player.GetAll() returns a num/seq table, it’s possible to just use a for loop to go from 1 all the way up to the number of entries in the table ( #tPlayers ), then use the iteration variable (i) to reference the player at that position in the table, instead of using pairs. Using a for loop in a situation like this is slightly quicker.

TL;DR - To actually answer the question, it’s better to use a for loop to iterate through a Lua table with sequential numeric keys, as opposed to pairs or next - it tends to be slightly faster. Here’s an example.

For sequential, numerical tables, the order of efficiency from least to most efficient for loop types is pairs, ipairs, then numerical. It’s a minimal efficiency save for smaller tables, but I personally always use numerical for loops for sequential tables to show that they are such.

Thanks you two.

But there are flags for that…

About flags that I completely forgot …: D

P.S. Thread can be closed, the question is solved