forbid to attack

Hi, FacePunch.
How to forbid to attack to players within 5 seconds after a spawn?

If assuming you mean you don’t want players to be attacked after they spawn for 5 seconds. You should enable godmode on a player when they spawn, and create a timer for 5 seconds, after which godmode is disabled.

Also, please use a translator.

I want that the player didn’t attack
Je veux pour que le joueur ne batte pas

I couldn’t beat(attack) other players

I meant translate into English properly, why translate into French?



function SoYouWantBabyGodSystem( ply, attacker )
     if(ply:GetNWInt("GodTime") < CurTime() then
          return true
     else
          print(attacker:Nick() .. "attacked a newspawn ("..ply:Nick()..")")
          return false
     end
end
hook.Add("PlayerShouldTakeDamage", "BabyGodSystem", SoYouWantBabyGodSystem)

hook.Add("PlayerSpawn", "AddGod", function(ply)
     ply:SetNWInt("GodTime", CurTime()+5)
end)



Use it at your peril.

EDIT:

Take a look at:

How to forbid to use a weapon (to shoot, to beat)?



function SoYouWantBabyGodSystem( ply, attacker )
     if ply:GetNWInt("GodTime") < CurTime() then
          return true
     else
          print(attacker:Nick() .. "attacked a newspawn ("..ply:Nick()..")")
          return false
     end
end
hook.Add("PlayerShouldTakeDamage", "BabyGodSystem", SoYouWantBabyGodSystem)

hook.Add("PlayerSpawn", "AddGod", function(ply)
     ply:SetNWInt("GodTime", CurTime()+5)
end)


Doesn’t work

We’ve already told you, “doesn’t work” is a shitty reply. Tell us why you think it doesn’t work, if you have any errors, which realm the errors are, and post the code that’s erroring.

First off, why did you bother returning true? This will make it so other hooks and the GM:PlayerShouldTakeDamage function will not run if you return true. Also, you put an extra parentheses in front of if, which is just a typo. Third, you either got your greater-than and less-than signs mixed up or you returned the wrong boolean both times. Finally, why did you bother networking an integer that only needs to be accessed serverside? This may fair better:



local SpawnProtectionTime = 5

hook.Add('PlayerSpawn', 'AddSpawnProtection', function(ply)
	ply.spawnprotection = CurTime() + SpawnProtectionTime
end)

hook.Add('PlayerShouldTakeDamage', 'DoSpawnProtection', function(ply, attacker)
	if ply.spawnprotection and ply.spawnprotection < CurTime() then
		print('Attack against '..ply:Name()..' negated; spawn protection active.') -- Use this to check if it works.
		return false
	end
end)


I see you want something more, but I can’t figure out what the hell you are talking about. Please speak English or use a translator properly, for god’s sake.

I don’t think it’s that hard to understand, he wants to stop people from shooting. Not from taking damage, but from actually shooting. And I don’t know if that’s going to be possible.

Huh, my best guess was that he wanted people to stop people from attacking based on what weapon they were using, but I suppose your answer makes more sense. He could stop people from firing by editing each and every weapon to not allow firing when conditions are met, but that’s inefficient and messy.

Something like this?
[lua]
if SERVER then
hook.Add(“StartCommand”, “StopAttack”, function( ply, mvd, ucmd )
if ply.cannotattack and mvd:KeyDown( IN_ATTACK ) then
mvd:RemoveKey( IN_ATTACK )
end
end)

hook.Add("PlayerSpawn", "StopAttacking", function(ply)
	ply.cannotattack = true
	ply:SetNWBool( "CanAttack", false )
	timer.Simple(5, function()
		if IsValid(ply) then
			ply.cannotattack = false
			ply:SetNWBool( "CanAttack", true )
		end
	end)
end)

else

hook.Add("CreateMove", "StopAttack", function( ucmd )
	if !LocalPlayer():GetNWBool( "CanAttack") and ucmd:KeyDown( IN_ATTACK ) then
		ucmd:RemoveKey( IN_ATTACK )
	end
end)

end
[/lua]

That would probably work, but it might just be better if we extend the spawn protection to deal with not only the receiver’s variable but also the attacker. This WOULD NOT prevent them from firing, but will instead prevent them from doing damage. I know this is not EXACTLY what he asked for, but it’s quite a bit simpler than messing with CUserCmd. Here’s an example:



local SpawnProtectionTime = 5

hook.Add('PlayerSpawn', 'AddSpawnProtection', function(ply)
	ply.spawnprotection = CurTime() + SpawnProtectionTime
end)

hook.Add('PlayerShouldTakeDamage', 'DoSpawnProtection', function(ply, attacker)
	if ply.spawnprotection and ply.spawnprotection < CurTime() then
		print('Attack against '..ply:Name()..' negated; spawn protection active.') -- Use this to check if it works.
		return false
	end
	if attacker.spawnprotection and attacker.spawnprotection < CurTime() then
		print(Format('Attack against %s by %s negated; spawn protection active.', ply:Name(), attacker:Name()))
		return false
	end
end)


Many thanks to everybody!
You very much everything helped!

[editline]20th April 2015[/editline]

Good job