SetVelocity not working?

Okay, here’s my Lua Script I have, and it’s not working, which doesn’t make any sense.



local isCreated = false

hook.Add("PlayerSpawn", "FixSpawners", function(ply)
timer.Simple(0.1,function()
ply.Energy = 100
end)
end)

hook.Add("KeyPress", "Healer", function(ply)
if ply:KeyDown(IN_USE) then
if ply.Mode == 0 then
local ent = ply:GetEyeTrace().Entity
if ent:IsValid() and (ent:Health() < 100) and (ply:GetPos():Distance(ent:GetPos()) < 75) and (ply.Energy > 40) then
ent:SetHealth(ent:Health() + 20)
ply.Energy = ply.Energy - 40
if ent:Health() >= 100 then 
ent:SetHealth(100)
ply:SetHealth(ply:Health() + 10)
end
end
end
if ply.Mode == 1 then
end
if ply.Mode == 2 then
if ply.Energy >= 20 then
ply:SetVelocity(Vector(0,0,500))
ply.Energy = ply.Energy - 20
end
end
end
end)

hook.Add("HUDPaint", "HealPaint", function()
local ply = LocalPlayer()
if isCreated == false then
surface.CreateFont("ScoreboardText", 65, 400, true, false, "testercd")
isCreated = true
end
if not ply.Energy then
ply.Energy = 100
end
if not ply.Retime then
ply.Retime = CurTime()
end
if not ply.IsPressing then
ply.IsPressing = false
end
if not ply.Mode then
ply.Mode = 0
end
if ply:KeyDown(IN_SPEED) and ply:KeyDown(IN_ZOOM) then
if ply.IsPressing == false then
ply.Mode = ply.Mode + 1
if ply.Mode >= 3 then
ply.Mode = 0
end
end
ply.IsPressing = true
else
ply.IsPressing = false
end
if ply:KeyDown(IN_USE) then
if ply.Energy <= 0 then
ply.Energy = 0
end
ply.Retime = CurTime() + 5
else
if CurTime() > ply.Retime then
ply.Energy = ply.Energy + 0.05
if ply.Energy >= 100 then
ply.Energy = 100
end
end
end
if ply.Mode == 0 then
draw.SimpleText("Mode: Healing", "testercd", 0, ScrH() - 210, Color(0,0,0,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
elseif ply.Mode == 1 then
draw.SimpleText("Mode: Running", "testercd", 0, ScrH() - 210, Color(0,0,0,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
elseif ply.Mode == 2 then
draw.SimpleText("Mode: Jumping", "testercd", 0, ScrH() - 210, Color(0,0,0,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end
draw.RoundedBox( 6, 30, ScrH() - 170, 190, 70, Color( 0, 0, 0, 75 ) )
draw.SimpleText("Energy", "ScoreboardText", 70, ScrH() - 120, Color(235,200,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(tostring(math.Round(ply.Energy)), "testercd", 150, ScrH() - 140, Color(235,200,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.RoundedBox( 6, 30, ScrH() - 90, 190, 70, Color( 0, 0, 0, 75 ) )
if ply:Health() <= 0 then
draw.SimpleText("Health", "ScoreboardText", 70, ScrH() - 40, Color(255,0,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(ply:Health(), "testercd", 150, ScrH() - 60, Color(255,0,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
else
draw.SimpleText("Health", "ScoreboardText", 70, ScrH() - 40, Color(235,200,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText(ply:Health(), "testercd", 150, ScrH() - 60, Color(235,200,0,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
end)
//{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}
function hidehud(name)
for k, v in pairs{"CHudHealth"} do
if name == v then return false end
end
end
hook.Add("HUDShouldDraw", "hidehud", hidehud)


So my problem lies within the KeyPress hook. When the mode is set to “2” (Jumping), it will then check if the player has enough energy (20 or more) if they do, Set the Velocity of the player to the Vector of (0,0,500) and decrease their energy by 20. Now, this is being executed, the player is losing energy, however, the player’s velocity is not being modified. They just stay still. If I move the “ply:SetVelocity(Vector(0,0,500))” above the whole if statement, then it works, oddly enough (With the exception that every single key makes you fly upwards). This lua file is just placed into the: “lua/autorun/” directory and runs perfectly fine if you were to test it out. The only thing is, the player does not fly up into the air when they are supposed to. If you have a solution, I’d be glad to hear it. Thanks!

you’re not defining which key the player needs to press.

it should look like this:
[lua]
– some code here
if ply:KeyDown(IN_JUMP) then
if ply.Mode == 2 and ply.Energy >= 20 then
ply:SetVelocity (Vector(0,0,500))
ply.Energy = ply.Energy - 20
end
end
[/lua]

Yeah, the big


if ply:KeyDown(IN_USE) then

statement is where it is called. I wanted it to be activated when the user pressed the “USE” button. Which it does, it’s just not setting the Velocity of the player. If you were to try the script, you would see that the Players Energy goes down by 20 when you press “use” while having the “jumping” mode activated.

Also, if you try EXACTLY what you did, the ply:SetVelocity(Vector(0,0,500)) part does not work. Which is why I created this thread. You can debug it, by placing a Msg(“Executed!”) infront,or underneath the ply:SetVelocity() statement, and it will be executed. ply:SetVelocity() is not though. Which is infuriating.

So I am to assume that this is too complex for any of the scripters to understand? =/

Nah, we probably just all missed your post. I’m pretty sure the main problem with is your script being executed clientside. The client can’t change a player’s velocity.

Also for some reasons you are detecting keypresses inside an HUDPaint hook which you should not be doing (These should only be used for drawing). Also since the HUDPaint hook has to be clientside it will not affect the serverside version of ply.Move. What you need to do is place all of your keypress code in a serverside file such as init.lua. Also rather then using KeyDown() you could simply use the second argument that is supplied to the KeyPress hook as detailed here :
**[Gamemode.KeyPress

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.KeyPress)**

:eng101:

The problem with that is that I don’t want to use seperate files, I want to just have a simple whatever.lua placed into the autorun directory. Well thanks for the answer, I guess I’ll just figure out something else.

If you want it to be in separate files it’s going to be very complicated but you can do it. What you can’t get away with is doing everything on the client or everything on the server, you will need to use both environments.