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
[CODE]function ENT:Use(activator, ply)
activator:SetHealth(activator:Health()+150)
ply:SetRunSpeed( 100 )
ply:SetWalkSpeed( 50 )
self.Entity:Remove()
end[/CODE]
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?
[CODE]function ENT:Use(activator, caller)
activator:SetHealth(activator:Health()+150)
activator:SetRunSpeed( 100 )
activator:SetWalkSpeed( 50 )
activator:SendHint( "Test!", 10)
self.Entity:Remove()
end
[/CODE]
But these didn't worked too.
Hum, someone can edit my code to show me how it works because I'm lost. :/
I didn't found the answer yet....Someone can help me by telling me how to use SpeedSet in an useable entity please?
[QUOTE=Akahon;20594323]More like these?
[CODE]function ENT:Use(activator, caller)
activator:SetHealth(activator:Health()+150)
activator:SetRunSpeed( 100 )
activator:SetWalkSpeed( 50 )
activator:SendHint( "Test!", 10)
self.Entity:Remove()
end[/CODE]
But these didn't worked too.
Hum, someone can edit my code to show me how it works because I'm lost. :/[/QUOTE]
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.
[CODE]
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[/CODE]
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
[CODE]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[/CODE]
Shared
[CODE]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[/CODE]
[CODE]
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
[/CODE]
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.
[CODE]while (used==1 && "playerIsAlive" )
{
thethingiwantodowith
}
removething[/CODE]
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?
[QUOTE=Akahon;20778217]
Someone can confirm that while loop exists in LUA?
[/QUOTE]
[lua]
while ( true ) do
print("batman!!1")
end
[/lua]
[QUOTE=Akahon;20778217]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.
[CODE]while (used==1 && "playerIsAlive" )
{
thethingiwantodowith
}
removething[/CODE]
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?[/QUOTE]
[lua]while ply:Alive() do
-- Code and shit here
end[/lua]
Easy enough :P
Thanks :D
"
[editline]09:16PM[/editline]
Now I'm searching a function to "wait" like
[CODE]
while blah = blah
wait 1 sec
functionlambda[/CODE]
[b][url=http://wiki.garrysmod.com/?title=Timer.Simple]Timer.Simple [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
here's some code you will find useful:
[code]while blah==blah
timer.Simple(1, functionlambda) --timer.Simple(INTEGER, FUNCTION)
[/code]
I tried to do that function with the timer but it don't works
[CODE]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
[/CODE]
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, [b][url=http://wiki.garrysmod.com/?title=Ents.Create]Ents.Create [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b].
Thanks but I don't know why my function doesn't work, especially in that while loop
[CODE]
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
[/CODE]
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 ([b][url=http://wiki.garrysmod.com/?title=Timer.Create]Timer.Create [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]) or a think hook ([b][url=wiki.garrysmod.com/?title=Gamemode.Think]Gamemode.Think [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] ) to manage reoccurring events, a while loop will just cause the game to hang while the loop runs.
Here's my new not-working code :confused:
[CODE]God = 0
function ENT:Use(activator, ply)
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)
God = 1
self.Entity:Remove()
end
Hplosetime = 0
function Godkill(activator, caller, ply)
if (God = 1) then
if (CurTime() >= Hplosetime then
if( activator:Health() <=2 )then
ply:Kill()
God = 0
return
else
activator:SetHealth(activator:Health()-1)
Hplosetime = CurTime() + 1
end
end
end
end
end
hook.Add("Think", "Godkill", Godkill)
[/CODE]
What's wrong?
Errors?
No, the entity is not spawning.
Is it in the menu? If it is, have you got a spawn function?
Yes
init
[CODE]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_godhat" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
return ent
end
function ENT:Initialize()
-- Set up the entity
self.Entity:SetModel("models/props_cans/soldier_viking.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
God = 0
function ENT:Use(activator, ply)
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)
God = 1
self.Entity:Remove()
end
Hplosetime = 0
function Godkill(activator, caller, ply)
if (God = 1) then
if (CurTime() >= Hplosetime then
if( activator:Health() <=2 )then
ply:Kill()
God = 0
return
else
activator:SetHealth(activator:Health()-1)
Hplosetime = CurTime() + 1
end
end
end
end
end
hook.Add("Think", "Godkill", Godkill)
[/CODE]
If the addon is registeted properly and it doesn't work then there are errors. It's as simple as that. Make sure to read the whole console.
Just because the errors don't pop up on the screen doesn't mean there aren't any, you need to check the console.
I've got this
[CODE]Couldn't register Scripted Entity immolate - the Type field is empty!
entities/sent_godhat/init.lua:50: ')' expected near '='
Folder = entities/sent_godhat
Couldn't register Scripted Entity sent_godhat - the Type field is empty!
Skipping E2 extension 'propcore'.
[/CODE]
Look at line 50, you are missing a closing bracket (parenthesis).
In fact there was an missing "=" in" if ( God == 1 )" too
Sorry, you need to Log In to post a reply to this thread.