G-Mod Lua Function Player touch?

Hello,

im looking for a longer time and through the wiki and cant find anything that helps me out to solve my problem.

Im looking for a function like :

If Player1 is touching Player2
do damage

I have 2 classes Player 1 and Player 2 and want to do some damage when they touch each other.

Thank you for your help.

Legit took me 3 seconds.

Oh Jesus…now i get it…Players are also just entitys… -.- damn sorry but thank you for your help !

That is for custom entities.

[editline]4th December 2016[/editline]

You’ll probably have to use a ShouldCollide hook to see when they are touching.

Hm ok thank you for this information…the only problem is i have so far no clue how to start with this ,

Check if the two teams touch each other and do damage, im reading at the moment. Thank you for your help.

Hacky way – there might be a better solution.


hook.Add("ShouldCollide", "TeamDamage", function(pEnt1, pEnt2)
   if (pEnt1:IsPlayer() and pEnt2:IsPlayer()) then
      -- code
   end
end )

If im not wrong i should add this into the init.lua correct?

I have done this so far


 AddCSLuaFile( "cl_init.lua" ) 
 AddCSLuaFile( "shared.lua" ) 
 
 include( 'shared.lua' ) 
 
 function GM:PlayerInitialSpawn( ply ) 
	 ply:SetTeam( 1 )
         ply:ConCommand( "team_menu" )
    
 end 
 
 function GM:PlayerLoadout(ply)
 
	ply:StripWeapons() 
 
	if ply:Team() == 1 then 
		ply:Give("weapon_physcannon") 
		ply:SetModel( "..." ) 
	elseif ply:Team() == 2 then
		ply:Give("weapon_physgun") 
		ply:SetModel( "..." ) 
	end 
 
end

 function team_1( ply ) 
 
     ply:SetTeam( 1 )
	 ply:Spawn()
 end 
 
 function team_2( ply ) 
 
     ply:SetTeam( 2 ) 
	 ply:Spawn()
 end 
 
 concommand.Add( "team_1", team_1 ) 
 concommand.Add( "team_2", team_2 )
 
 
 hook.Add("ShouldCollide", "TeamDamage", function(pEnt1, pEnt2)
   if (pEnt1:IsPlayer() and pEnt2:IsPlayer()) then
      ply:SetHealth(ply:Health()-5)              
   end 
end )

I join in Team 2 and add a bot to team 1, but if we run into each other nothing is happening.

Because you are trying to set the health of something that doesn’t exist.
(And you should be using takedamage, because it kills the player when their health reaches 0)

Hello, here are my 3 lua files so far i cant figure out how to make team 1 and team 2 dealing damage by touching each other, hope someone can help me out here. Its all basic because i just started to programm a gamemode for gmod.

CL_INIT.LUA



include( "shared.lua" )

 function set_team() 
 
 frame = vgui.Create( "DFrame" ) 
 frame:SetPos( 100, ScrH() / 2 ) 
 frame:SetSize( 200, 210 ) 
 frame:SetTitle( "Change Team" )
 frame:SetVisible( true ) 
 frame:SetDraggable( false ) 
 frame:ShowCloseButton( true ) 
 frame:MakePopup() 
 
 team_1 = vgui.Create( "DButton", frame ) 
 team_1:SetPos( 30, 30 )
 team_1:SetSize( 100, 50 ) 
 team_1:SetText( "Team 1" ) 
 team_1.DoClick = function() 
 
     RunConsoleCommand( "team_1" ) 
 
 end 
 
 team_2 = vgui.Create( "DButton", frame ) 
 team_2:SetPos( 30, 85 ) 
 team_2:SetSize( 100, 50 ) 
 team_2:SetText( "Team 2" ) 
 team_2.DoClick = function()
 
     RunConsoleCommand( "team_2" ) 
 
 end 
 
 end 
 concommand.Add( "team_menu", set_team ) 

INIT.LUA


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

 function GM:PlayerInitialSpawn( ply ) 
	 ply:SetTeam( 1 )
     ply:ConCommand( "team_menu" )
     
 end 
 
 function GM:PlayerLoadout(ply)
 
	ply:StripWeapons() 
 
	if ply:Team() == 1 then 
		ply:Give("weapon_physcannon") 
		ply:SetModel( "models/Rosa/Rosa_Player.mdl" ) 
	elseif ply:Team() == 2 then
		ply:Give("weapon_physgun") 
		ply:SetModel( "models/player/kuristaja/pbear/pbear.mdl" ) 
	end 
 
end -- This ends the function.

 function team_1( ply ) 
 
     ply:SetTeam( 1 ) //Make the player join team 1 
	 ply:Spawn()
 end 
 
 function team_2( ply ) 
 
     ply:SetTeam( 2 ) //Make the player join team 2 
	 ply:Spawn()
 end 
 
 concommand.Add( "team_1", team_1 ) 
 concommand.Add( "team_2", team_2 ) 
 
 

SHARE.LUA


 
 GM.Name 		= "Test" //Set the gamemode name 
 GM.Author 		= "Test" //Set the author name 
 GM.Email 		= "Test" //Set the author email 
 GM.Website 	= "Test" //Set the author website 
 
 team.SetUp( 1, "Guest", Color( 125, 125, 125, 255 ) ) 
 
 team.SetUp( 2, "Another Guest", Color( 225, 225, 0 , 225 ) ) 


Did you not read what I posted?

Hello

i tried now so many things, i cant figure it out how to detect when 2 players collide into eachother,


 function GM:ShouldCollide( Entity ent1, Entity ent2 ) 
	if ent1:IsPlayer and ent2:IsPlayer
	PrintMessage( HUD_PRINTTALK, "I'm new here." )
	end

or


 hook.Add("ShouldCollide", "TeamDamage", function(pEnt1, pEnt2)
   if (pEnt1:IsPlayer() and pEnt2:IsPlayer()) then
    PrintMessage( HUD_PRINTTALK, "I'm new here." )
   end 
end )

i dont know what i can do from here…thank you for your help so far.

Make sure to call player:SetCustomCollectionCheck(true) (in PlayerInitialSpawn for example), so ShouldCollide is ran.

Fruitwesp thank you so much, this fixed my problem! Thank you soooo much.

But now i have the problem that when im even not close to an other player then this function is switched on, kinda like with a msg “im close” but im not realy near the player. Is there a way to set the size of this Collision?

Most would assume there is some sort of script floating around for a generic problem such as this…

So far i got this now, when the player is close to the other we got the msg “Im new here”,


function GM:PlayerInitialSpawn( ply ) 
	ply:SetTeam( 1 )
    ply:ConCommand( "team_menu" )
	ply:SetCustomCollisionCheck(true) 
 end 
 


 hook.Add("ShouldCollide", "TakeDamage", function(pEnt1, pEnt2)
   if (pEnt1:IsPlayer() and pEnt2:IsPlayer()) then
    PrintMessage( HUD_PRINTTALK, "I'm new here." )
   end 
end )


but as i said its not working as it should be, i would like to change this to two teams

Something like :


If Team1 and Team2 is touching each other
then
kill player from Team1
end


wouldn’t


 hook.Add("ShouldCollide", "TakeDamage", function(pEnt1, pEnt2)
   if (pEnt1:IsPlayer() and pEnt2:IsPlayer()) then
    pEnt1:RunConsoleCommand("kill")
   end 
end )


work?

as pEnt1 is still a player and RunConsoleCommand can be directed to players

[editline]7th December 2016[/editline]

You need to reduce the radius of detection somehow.

maybe using this. http://wiki.garrysmod.com/page/Entity/GetModelRadius

Talking a bit out of my ass here but wouldn’t using the PlayerSpawn hook to set ENT:StartTouch, EndTouch and Touch work?

Just have hooks in the players themselves when another ent(if ply) touches them then they start taking damage.

As this post has already said in response to you,

So make the ply object custom. I’m sure there is some way to add a function to the object.

Hm to ply object custom ? dont know how and i dont have a clue how to set the model radius, im still thinking about it…but so far no clue , but thank you for your help so far