Get a player command - Sprint

Hello

How can I get a console command which is using by the player. Now it is +speed and -speed.
Please help :slight_smile:

Do you mean this?

[lua]
if (ply:KeyDown(IN_SPEED )) then

[/lua]

Maybe

maybe?

So you want something to say if they are in +speed?

If so, this is it:
[lua]
function PlayerRunning(ply)
if (ply:KeyDown(IN_SPEED )) then
print(“Im Running!”) // This is the bit you edit
return end
hook.Add( “Think”, “PlayerRunning”, PlayerRunning )
[/lua]

Thanks

Alternatively you can add a concommand to “+speed”

lol, it is work but it is buggy,

[LUA]
ply = LocalPlayer()

if (ply:KeyDown(IN_SPEED ) ) then

if stamina > 0 then

	timer.Create("down", 0.5, 0, function()
	stamina=stamina-1
	draw.DrawText(stamina,"Default",ScrW()-80,20)
	print(stamina)
	end )		
	timer.Stop("up")
	timer.Start("down")
else

timer.Start("down")	

end

else

if stamina < 100 then

	timer.Create("up", 0.5, 0, function()
	stamina=stamina+1
	draw.DrawText(stamina,"Default",ScrW()-80,20)
	print(stamina)
	end )
	timer.Stop("down")
	timer.Start("up")	
	
else

timer.Stop("up")

end

end
[/LUA]

Output:

46
45
44
43
42
41
40
39
38
37
36
35
34 - Here I opened the console
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

First of all you need that code in a Think function.

Second it can’t all be serverside/clientside, I suggest you make it serverside to start with. (So don’t use draw.DrawText)

Thirdly create the timer once in the PlayerSpawn hook, and apply it a proper UNIQUE ID. e.g.

[lua]
hook.Add(“PlayerSpawn”, “lolthisisplayerspawn”, function(ply)
ply.Stamina = 100
timer.Create(ply:UniqueID()…“stamina_down”, 0.5, 0, function()
ply.Stamina = ply.Stamina - 1
end)
timer.Stop(ply:UniqueID()…“stamina_down”)
end)
[/lua]

Forth… Eh just copy this:

[lua]
hook.Add(“PlayerSpawn”, “lolthisisplayerspawn”, function(ply)
ply.Stamina = 100
timer.Create(ply:UniqueID()…“stamina_down”, 0.5, 0, function()
ply.Stamina = ply.Stamina - 1
end)
timer.Stop(ply:UniqueID()…“stamina_down”)

timer.Create(ply:UniqueID()…“stamina_up”, 1, 0, function()
ply.Stamina = ply.Stamina + 1
end)
timer.Stop(ply:UniqueID()…“stamina_up”)
end)

hook.Add(“Think”, “HandyThinkFunction”, function()
for _, ply in pairs(player.GetAll()) do
hook.Call(“PlayerThink”, GAMEMODE, ply)
end
end)

hook.Add(“PlayerThink”, “HandleStaminaInput”, function(ply)
if ply:KeyDown(IN_SPEED) then
timer.Start(ply:UniqueID()…“stamina_down”)
timer.Stop(ply:UniqueID()…“stamina_up”)

  ply:SetRunSpeed(500)

else
timer.Stop(ply:UniqueID()…“stamina_down”)
timer.Start(ply:UniqueID()…“stamina_up”)

  ply:SetRunSpeed(300)

end
end)
[/lua]

[lua]
hook.Add(“Think”, “StaminaThink”, function()
for _, p in pairs(player.GetAll()) do
p.Stamina = p.Stamina or 100;
if( p:KeyDown(IN_SPEED) ) then
p.Stamina = math.Clamp(p.Stamina-1,0,100)
else
p.Stamina = math.Clamp(p.Stamina+1,0,100)
end
if( p.Stamina > 0 ) then
p:SetRunSpeed(500)
else
p:SetRunSpeed(250)
end
end
end)
[/lua]

iRzilla you forgot to slow them down if stamina is 0

Oopsy, thank you. :shobon:

Please, help in a new thing.
function SendData2(ply)
SendUserMessage(“my_message”, ply, “hai”, 1337)
end

How does it work?

It send a signal to the client. On the client, you use:
[lua]
function ReceiveData2(um)
Str = um:ReadString()
Short = um:ReadShort()
end
[/lua]

Why is it bad please???

[LUA]
function UnderWater(ply)

if (ply:WaterLevel() > 0) then

	if (ply:KeyDown(IN_USE) ) then
	
	Water = math.random(8,15)
	
		if (ply.Thirst + Water > 100) then
		
		ply.Thirst = 100
		
		else
		
		ply.Thirst = ply.Thirst + Water
		
		end
	
	
	
	end
end

end

hook.Add(“Think”,“UnderWater”,UnderWater)
[/LUA]

What are you trying to do? :\

Can’t use ply argument in Think.

[lua]
hook.Add(“Think”, “AddThirst”, function()

for k,v in pairs(player.GetAll()) do

if( v:WaterLevel() > 0 && v:KeyDown(IN_USE) ) then

  local water = math.random(8,15)

  if( v.Thirst + water > 100 ) then

    v.Thirst = 100;

  else

    v.Thirst = v.Thirst + water;

  end

end

end

end)
[/lua]