[Entity]Problem with Speed setting

I’m coding an entity which when used will increase the health and lose some speed.
“Health add” works but “Speed loss” don’t.
Here’s the code


function ENT:Use(activator, ply)
activator:SetHealth(activator:Health()+150)
ply:SetRunSpeed( 100 )
ply:SetWalkSpeed( 50 )
self.Entity:Remove()
end

Someone know why it don’t works?

By the way, I’m searching the “trigger” (like activator) for the player death.

Please help me :’{

Yes your functions fails because you try to set the speed of ply, which is the caller??? Set the speed on the activator and check if it is a player beforehand to be sure.

More like these?


function ENT:Use(activator, caller)
activator:SetHealth(activator:Health()+150)
activator:SetRunSpeed( 100 )
activator:SetWalkSpeed( 50 )
activator:SendHint( "Test!", 10)
self.Entity:Remove()
end
	

But these didn’t worked too.
Hum, someone can edit my code to show me how it works because I’m lost. :confused:

I didn’t found the answer yet…Someone can help me by telling me how to use SpeedSet in an useable entity please?

Uhhh, I plugged this code into an entity for the heck of it, and it seems to work just fine…

Technically this is a better way of doing it, but either way worked fine for me. Just plug your code into an init.lua and you should be fine.



function ENT:Use(activator, caller)
 if activator:IsPlayer() then
  activator:SetHealth(activator:Health()+150)
  activator:SetRunSpeed( 100 )
  activator:SetWalkSpeed( 50 )
  activator:SendHint( "Test!", 10)
  self.Entity:Remove()
 end
end

Well, I tried to test and I had the same result as before:
Health works but speed doesn’t change and the hint is appearing after 10 seconds and displays “#Hint Test!” or something like that but the hint doesn’t show up if I use a second entity.

At this point would you mind pasting your whole ent’s code here? The code you have should work so the issue might be something else.

init


AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SpawnFunction( ply, tr )
	
	if !tr.Hit then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 1

	local ent = ents.Create( "sent_armyhat" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
end

function ENT:Initialize()

	-- Set up the entity
	self.Entity:SetModel("models/props_cans/spy_bill.mdl")
 
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	
	self.Index = self.Entity:EntIndex()
	
	local phys = self.Entity:GetPhysicsObject()
	if phys:IsValid() then
		phys:Wake()
	end
end

function ENT:Use(activator, caller)
 if activator:IsPlayer() then
  activator:SetHealth(activator:Health()+150)
  activator:SetRunSpeed( 100 )
  activator:SetWalkSpeed( 50 )
  activator:SendHint( "Test!", 10)
  self.Entity:Remove()
 end
end

Shared


ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.PrintName = "Army hat"
ENT.Author = "Akahon"

ENT.Spawnable = true
ENT.AdminSpawnable = true 

ENT.Category		= "Mahsent";
ENT.Information		= "A hat to get ultra health and lose some speed"


function ENT:SetupModel()

	self.Entity:SetModel( "models/props_cans/spy_bill.mdl" )

end



function ENT:Initialize()
 self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" ) 
 self.Entity:SetName("Gravity Test Entity")
 self.Entity:PhysicsInit( SOLID_VPHYSICS )
 self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
 self.Entity:SetMoveCollide(MOVECOLLIDE_FLY_SLIDE)
 self.Entity:SetSolid( SOLID_VPHYSICS )
 self.Entity:SetUseType(SIMPLE_USE)
 
 local phys = self.Entity:GetPhysicsObject()
 if (phys:IsValid()) then
  phys:Wake()
  phys:EnableGravity(false)
  phys:EnableDrag(false)
  phys:EnableCollisions(true)
  self.Entity:StartMotionController()
 end
 self.Entity:SetKeyValue("rendercolor", "255 255 255")
 self.OldTime = CurTime()
end
 
function ENT:SpawnFunction( ply, tr )
 if ( !tr.Hit ) then return end
 
 local SpawnPos = tr.HitPos + tr.HitNormal * 16 + Vector(0,0,50)
 
 local ent = ents.Create( "gravitytestentity" )
 ent:SetPos( SpawnPos )
 ent:Spawn()
 ent:Initialize()
 ent:Activate()
 ent.SPL = ply
 return ent
end
 
function ENT:Use(activator, caller)
 if activator:IsPlayer() then
  activator:SetHealth(activator:Health()+150)
  activator:SetRunSpeed( 100 )
  activator:SetWalkSpeed( 50 )
  activator:SendHint( "Test!", 10)
  self.Entity:Remove()
 end
end


Is what I have. That should include all the functional stuff. The motion control stuff can be stripped out.

In fact, my entity was conflicting with the “player resizer addon”(that addon conflicts with everything I think… even multiplayer…). So my ent set the player’s speed but now, I want to know how can I make a thing like that.


while (used==1 && "playerIsAlive" )
{
thethingiwantodowith
}
removething

Someone can confirm that while loop exists in LUA?
By the way I don’t know how is called the “playerIsAlive” thing I need in Gmod, someone can tell me what it is?

[lua]
while ( true ) do
print(“batman!!1”)
end
[/lua]

[lua]while ply:Alive() do

– Code and shit here

end[/lua]

Easy enough :stuck_out_tongue:

Thanks :smiley:

"

[editline]09:16PM[/editline]

Now I’m searching a function to “wait” like



while blah = blah
wait 1 sec
functionlambda

**[Timer.Simple

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Timer.Simple)**
here’s some code you will find useful:


while blah==blah
timer.Simple(1, functionlambda) --timer.Simple(INTEGER, FUNCTION)


I tried to do that function with the timer but it don’t works


function ENT:Use(activator, ply)
local ss2 = activator:GetNetworkedFloat("SprintSpeed")
local ws2 = activator:GetNetworkedFloat("WalkSpeed")
local ss = activator:GetNetworkedFloat("SprintSpeed")
local ws = activator:GetNetworkedFloat("WalkSpeed")
activator:SetHealth(activator:Health()+400)
ply:SetWalkSpeed(ws*2)
ply:SetRunSpeed(ss*2)
ply:SetGravity(0.2)
local God = 1
self.Entity:Remove()
while (God == 1 && ply:Alive() ) do
if( activator:Health() <=2 )then
			ply:Kill()
	return
else
timer.Simple(1, activator:SetHealth(activator:Health()-1)
end
end
end
end


By the way, what should I do to spawn in an entity’s function an other entity I created in the addon?

The same thing you do to spawn any entity, **[Ents.Create

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

Thanks but I don’t know why my function doesn’t work, especially in that while loop



while (God == 1 && ply:Alive() ) do
if( activator:Health() <=2 )then
			ply:Kill()
	return
else
timer.Simple(1, SetHealth(activator:Health()-1)
end
end
end
end


Someone can fix it? Or tell me what’s wrong?

Why are you using a while loop?

[editline]09:10AM[/editline]

You should be using timers (**[Timer.Create

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Timer.Create)) or a think hook ([Gamemode.Think

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.Think)** ) to manage reoccurring events, a while loop will just cause the game to hang while the loop runs.