How to get a player object knowing its entity?

I have an entity object got by GetEyeTrace() function. If it’s a player, I have to ban him calling Ban() function.
But this function needs a player object as an argument. How I can get it?

Use the entity itself. Just check if the entity is a valid player with **[Entity.IsPlayer](** and you’re good to go.

I already tried to do so, but it gives no effect.

	local trace = self.Owner:GetEyeTrace()
	if (trace.HitPos:Distance(self.Owner:GetShootPos())<=100 and !trace.Entity:IsWorld() and SERVER)  then
		ent = self.Owner:GetEyeTrace().Entity
		ent:TakeDamage(self.Damage, self.Owner, self.Owner:GetActiveWeapon())
		if (ent:IsPlayer()) then
		ent:Ban(bantime, "")

Replace line 3 with

local ent = trace.Entity

Right now, you are doing a second EyeTrace instead of getting the Entity from the first trace.

Doesn’t work again.

I would be able to help more if you gave your code in [lua] tags instead of


Any errors? Are you close enough to the player? Where are you calling this?

No errors. Close enough. From SWEP:PrimaryAttack() in shared.lua.

Doesn’t Ban just ban the player without kicking? I’m fairly sure you need to kick them as well.

You do.

Uhm. Bump.
As I got, ban code just doesn’t run. Any ideas why this happen?

Certain code tag fucked up my formatting. Not going to rewrite it. Do the kick thing, if that still doesn’t work, just put print() messages everywhere something could go wrong.

I think as previously stated, you have to kick the player also.

You might want to test this on a dedicated server with some friends, and not on your local machine with bots, as it may not work the same way.

In my experience, alot of my code behaves very differently on a listen server compared to a dedicated server.

throw this on the next line under your ‘:Ban()’ code.