SEnt using Chat Commands (using string.Explode)

Still working on my floating SEnt, and I can make it float, but it just sits there 'cause I still haven’t found out how to make it bob up and down like I wanted.

I was thinking I could do it with timers, but I can’t work with timers to save my life.

This is what I have:

[lua]
function ENT:PhysicsUpdate(phys)
timer.Create(“floatytimer”, 1, 1, Floaty, phys, 50) //makes the SEnt float upwards as defined in Floaty()
//timer.Stop(“floatytimer”)
end
[/lua]

I have no idea how to get the timer to run, and then reset itself and run the function again after a short while.

With the timer.Stop(“floatytimer”) included, nothing happens.
With it removed, the SEnt begins to float up at a constant rate, but it overloads GMod and CTDs when I remove it.

So I know how to make the thing move and down using SetVelocity(), but not how to make the timer work so it can alternate directions (float up for a second, then float back down, and back up again, etc.)

target is a table of entities.

**[Ents.FindByClass

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

[editline]09:22PM[/editline]

You are trying to call a function on a table of entities.

I just tried changing that line to
[lua]local target = chat_comm[2].GetClass()[/lua]

And now I get this error from that line:

chat_comm[2] is a string, not an entity.

[editline]09:29PM[/editline]

You were doing the right thing before with ents.FindByClass(), you just need to select an entity from the table. You could do this with table.Random(target) to select a random one.

[editline]09:31PM[/editline]

Also, IsValid is a method on the entity metatable, so you should be calling it with a colon (:slight_smile: like this:
[lua]entity:IsValid()[/lua]

Using table.Random(target) gives me this error:

Edit:
Fixed that by adding brackets around the ents.FindByClass(chat_comm[2]), but now I’m getting a new error from line 52:

[lua]
function skull_cmds (self, command)
local chat_comm = string.Explode(" ", command)
if (chat_comm[1] == “!target”) then
if(!chat_comm[2]) then
PrintMessage(HUD_PRINTTALK, “Please define a target!”) return end
local target = {ents.FindByClass(chat_comm[2])}
local atk = table.Random(target)
if(!atk) then return end
if(!atk:IsValid()) then return end // Line 52
Msg("VALID TARGET
")
//self.AttackTarget(target)
return end
return
end
[/lua]

I know I screwed something up, somewhere in there…

You need to check whether the table is empty before you do that. Something like this:
[lua]if #target == 0 then return end[/lua]

[editline]09:46PM[/editline]

Don’t put brackets around ents.FindByClass

[editline]09:47PM[/editline]

:doh:

Removed the brackets, added if (#target == 0) then return end

Okay, so ents.FindByClass(chat_comm[2]) isn’t returning anything, I guess.

It is probably returning an empty table because there aren’t any entities with the class that you are entering with the chat command.

ahh… yeah, that’s gotta be it.

I was typing “!target VladiMatt” (VladiMatt being my ingame name)

so I tried “!target *” and it printed ‘VALID TARGET’ in console, as commanded.

Here’s the current code
[lua]
function skull_cmds (self, command)
local chat_comm = string.Explode(" ", command)
if (chat_comm[1] == “!target”) then
if(!chat_comm[2]) then
PrintMessage(HUD_PRINTTALK, “Please define a target!”) return end
local target = ents.FindByClass(chat_comm[2])
if (#target == 0) then return end
local atk = table.Random(target)
if(!atk) then return end
if(!atk:IsValid()) then return end
Msg(atk)
//self.AttackTarget(target)
return end
return
end[/lua]

Now, it outputs the targeted entity in console. I tried !target player and it finally targeted me.

Now what I need to know is, how can I type !target [name of a player] and have it target that player?

(By the way, thanks so much for your help so far, it’s been really useful! :D)

There isn’t a built in method of finding a player by its name, but you can use this code:
[lua]function player.FindByName(name)
for _, ply in ipairs(player.GetAll()) do
if ply:Nick():lower():find(name:lower()) then
return ply;
end
end
end[/lua]

Use player.FindByName instead of ents.FindByClass, it will return a single player entity, not a table like before.

Fail code is fail.

Any reason why it is fail?

[editline]03:05PM[/editline]

Or do you just like being an ass?

Cubar - fail troll is fail.

It works perfectly! Thanks for your help!

My pleasure :smile:

Yup, this defiantly works thanks.

What is your problem?

Nothing why?

You are acting like an ass. I was just wondering why.

C’mon guys, don’t start this.

I’ve got a new problem, explained in the first post.

[lua]local phys = self:GetPhysicsObject()
phys:SetVelocity(phys:GetVelocity() + Vector(0,0,2math.cos(4CurTime())))[/lua]

In think.

Not the best way, but it is the easiest.