I'm trying to restrict player on player collision for a certain job of mine, but I can't figure out the function. I can't find anything online either, can someone please help me out?
[QUOTE=Splugie;52425815]I'm trying to restrict player on player collision for a certain job of mine, but I can't figure out the function. I can't find anything online either, can someone please help me out?[/QUOTE]
Player collision between players?
[editline]2nd July 2017[/editline]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/ShouldCollide]GM:ShouldCollide[/url]
[editline]2nd July 2017[/editline]
[QUOTE=highvoltage;52426321]Player collision between players?
[editline]2nd July 2017[/editline]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/ShouldCollide]GM:ShouldCollide[/url][/QUOTE]
yeah player collision between players
Check that link
[QUOTE=gonzalolog;52426586]Check that link[/QUOTE]
How would I phrase that into my job though? I'm not too experienced in creating functions
[QUOTE=Splugie;52426598]How would I phrase that into my job though? I'm not too experienced in creating functions[/QUOTE]
[CODE]hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
-- first check if the entities are valid and are players
-- then check if ent1's team is equal to ent2's team
-- if so return false so that they do not collide
-- if they arent equel then return true
end)[/CODE]
Here is some sudo code for you... And here are some functions you will need to use,
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsValid]IsValid[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/IsPlayer]Entity:IsPlayer[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Team]Player:Team[/url]
[QUOTE=Splugie;52426598]How would I phrase that into my job though? I'm not too experienced in creating functions[/QUOTE]
I don't know too much about darkrp but you will have to put that code outside of your job. Are you a server owner making a new job?
[QUOTE=MrRalgoman;52426799][CODE]hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
-- first check if the entities are valid and are players
-- then check if ent1's team is equal to ent2's team
-- if so return false so that they do not collide
-- if they arent equel then return true
end)[/CODE]
Here is some sudo code for you... And here are some functions you will need to use,
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsValid]IsValid[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/IsPlayer]Entity:IsPlayer[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Team]Player:Team[/url][/QUOTE]
Thanks a bunch
[editline]3rd July 2017[/editline]
[QUOTE=highvoltage;52427536]I don't know too much about darkrp but you will have to put that code outside of your job. Are you a server owner making a new job?[/QUOTE]
Yeah I am, I was trying to make it job specific though, for a single job
[QUOTE=MrRalgoman;52426799][CODE]hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
-- first check if the entities are valid and are players
-- then check if ent1's team is equal to ent2's team
-- if so return false so that they do not collide
-- if they arent equel then return true
end)[/CODE]
Here is some sudo code for you... And here are some functions you will need to use,
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsValid]IsValid[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/IsPlayer]Entity:IsPlayer[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Team]Player:Team[/url][/QUOTE]
You really shouldn't return true in ShouldCollide (and many other hooks) if the requirements for returning false aren't met.
Returning a value in a hook prevents other hooks of the same type from running, meaning only one of them will work.
For example:
One hook returns false if both entities are players and are on the same team, true otherwise.
Another returns false if one entity is a grenade and the other entity is a prop, true otherwise.
Only one of these hooks will actually work, the other one will never be called.
[QUOTE=AwfulRanger;52428683]You really shouldn't return true in ShouldCollide (and many other hooks) if the requirements for returning false aren't met.
Returning a value in a hook prevents other hooks of the same type from running, meaning only one of them will work.
For example:
One hook returns false if both entities are players and are on the same team, true otherwise.
Another returns false if one entity is a grenade and the other entity is a prop, true otherwise.
Only one of these hooks will actually work, the other one will never be called.[/QUOTE]
I'm so confused... Is it possible to create a single job that doesn't collide with players??
[QUOTE=AwfulRanger;52428683]You really shouldn't return true in ShouldCollide (and many other hooks) if the requirements for returning false aren't met.
Returning a value in a hook prevents other hooks of the same type from running, meaning only one of them will work.
For example:
One hook returns false if both entities are players and are on the same team, true otherwise.
Another returns false if one entity is a grenade and the other entity is a prop, true otherwise.
Only one of these hooks will actually work, the other one will never be called.[/QUOTE]
I probably could have worded the sudo code a bit better, this is how I would go about doing it.
[CODE]hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
if (!IsValid(ent1) || !IsValid(ent2)) then return true end
if (!IsPlayer(ent1) || !IsPlayer(ent2)) then return true end
if (ent1:Team() != ent2:Team()) then return true end
return false
end)[/CODE]
[QUOTE=MrRalgoman;52428754]I probably could have worded the sudo code a bit better, this is how I would go about doing it.
[CODE]hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
if (!IsValid(ent1) || !IsValid(ent2)) then return true end
if (!IsPlayer(ent1) || !IsPlayer(ent2)) then return true end
if (ent1:Team() != ent2:Team()) then return true end
return false
end)[/CODE][/QUOTE]
The way hooks work is basically like this: run through all hooks until one returns a value.
By guaranteeing a return in your hook, you're ensuring that no other hook will run after yours.
It's bad practice, because it can break many addons that also use these hooks.
[QUOTE=AwfulRanger;52428813]The way hooks work is basically like this: run through all hooks until one returns a value.
By guaranteeing a return in your hook, you're ensuring that no other hook will run after yours.
It's bad practice, because it can break many addons that also use these hooks.[/QUOTE]
So, this?
[CODE]
hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
if (IsValid(ent1) && IsValid(ent2)) then
if (IsPlayer(ent1) && IsPlayer(ent2)) then
if (ent1:Team() == ent2:Team()) then
return false
end
end
end
end)[/CODE]
[QUOTE=MrRalgoman;52428902]So, this?
[CODE]
hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
if (IsValid(ent1) && IsValid(ent2)) then
if (IsPlayer(ent1) && IsPlayer(ent2)) then
if (ent1:Team() == ent2:Team()) then
return false
end
end
end
end)[/CODE][/QUOTE]
Generally just don't return anything outside of an if statement.
[code]
hook.Add("ShouldCollide", "checkIfTeamsShouldCollide", function(ent1, ent2)
if (!IsValid(ent1) || !IsValid(ent2)) then return true end
if (!IsPlayer(ent1) || !IsPlayer(ent2)) then return true end
if (ent1:Team() != ent2:Team()) then return true end
return false --so just remove this part here
end)
[/code]
Sorry, you need to Log In to post a reply to this thread.