wiki: ents.FindInSphere
using ents.FindInSphere in this code:
hook.Add( "EntityTakeDamage", "ReduceFallDamage", function( target, dmginfo ) --every time damage is taken checks to see
if target:IsPlayer() and target.reduceDamage and dmginfo:IsFallDamage() and target.hasshot then --if it is fall damage, if the user has the weapon,
dmginfo:SetDamage( 0 )
print(target) --if the weapon has been fired etc.. if so then negate fall damage
timer.Simple(0.002, function() shock(target) end) --and initiate shock function
end
end )
function shock(owner) --shock function that produces sound, effects and damage to players in radius
print(owner)
local zaptable = {
"ambient/energy/zap1.wav",--generate table of shock sounds
"ambient/energy/zap2.wav",
"ambient/energy/zap3.wav"
}
for _,ply in pairs( ents.FindInSphere( owner:GetPos(), 350 ) ) do --find players in a sphere or size 350
if ply:IsPlayer() then
print(ply)
Does not find all players in the radius as shown in the photo the user of the SWEP is in red and the only person he kills is green despite all of there people in the picture being within the 350 radius of ents.FindInSphere.
Am i just doing this in the wrong way and i need to change to say use player tables and get pos or is this just a random bug. Thanks.
https://files.facepunch.com/forum/upload/107139/cc869aef-a12c-42f6-838f-b134de38c313/20180307213213_1.jpg
Try using a variable for the table, might be some timing error.
Something like:
local tab = ents.FindInSphere()
for k,v in pairs(tab)
instead of just doing
for k,v in pairs(ents.FindInSphere())
Either that or entity positions are just weird. Every player's position is at his feet, so you make a sphere at bottom of your feet and unless it finds that the position of this guy (The position of his feet) is inside the sphere then it won't return him. Here's a professional graph as an example:
https://i.gyazo.com/97c2df78b6613570c0e46c2a8a820b40.png
As you can see, even if the bottom guy looks closer to you, his position technically is really far away and even if the top guy looks further away he's actually close enough to be included in the table. The dots are their positions
That drawing has just made my day xD
Congrats you win the award for one of the most helpful people. That drawing shows that ents.FindInSphere is really bad for my purpose so I'll try using player.getall and go through them ipairs or something unless you have a better idea of what would function. Thank you very much.
Try this
for k,v in pairs(player.GetAll()) do
if(owner:GetPos():Distance(v:GetPos()) < 350) then
-- do whatever
end
end
Ill try that in the morning thanks for the help
Instead of GetPos you can get the center of the player bounding box, but checking for the distance in a player.GetAll for loop is a better solution
Okay so new issue. i changed the code to be:
for k,v in pairs(player.GetAll()) do
local owpos = owner:GetModelBounds()
local plypos = v:GetModelBounds()
if owpos:Distance(plypos) < 400 then--find players in a sphere or size 400
print(v)
But there are some issues when i test it. When i use the weapon it finds no one in the radius (i have 2 friends testing with me) When my friend Rad uses the hammer It finds both of us. and when my friend Eragon uses it it only kills me. This is super strange and i wonder if it is an issue with timing and might need a table. Wondering if anyone else has had this issue and whether its a limitation of the engine. thanks again
You are not supposed to be using GetModelBounds, but OBBCenter.
that shouldnt matter because at the moment i was just using :GetPos() but it would still be the same with the other issue in the first part of my reply
I like to use Entity/WorldSpaceCenter to save some space
Distance is an expensive function. Do
v:DistToSqr() < (dist^2)
This is not the issue I'm having. I'm confused about why it only finds players when a certain player uses the weapon and why it also has an issue with who hosts? I can post my full code if you guys really want because it works fine other than the issue I described.
Bump still having an issue with this and i have no idea why
Welcome to the world of Object Oriented Programming and Source games...
What's the problem now? Is it still picking up people that are further than some others? You can maybe try to use a bigger spere to find all players that are close and then use a tracer to see if the hitbox is inside the real sphere. I don't know just throwing some ideas :-I
To be honest I did something similar with my Rollout Pokemon Move but it's really hacky, I just have an entity that collides with only players, but I do not recommend it.
If it's killing the same people regardless of distance, it sounds like it's only being called once. Can you repost the hook where it's being called?
The only issue is that only certain people can use it. When tested on a local host with 3 players. When player 1 used it, it located and killed both player 2 and player 3. When player 2 used it it only located and killed player 3. And when player 3 used it, it didnt locate or kill anyone. It didnt seem to matter whether the person was traitor or not it just wouldnt kill them (It's a TTT SWEP). I can post the full code or even show you in a game if you want because there is not reason i see why it shouldnt work. Thanks once again
Now that's odd, might be a problem with file managing or just networking in general, if you have any of those. Yes, the full code would help, maybe even a screenshot of the folders you have the files in.
Ill send my code when I'm home
Follow this link to pastebin: [Lua]
Biggest reason why it doesn't find the players could be some wonky physics caused by the player going at high speed velocities when it happens.
We should have to rework on the entire code too because of how hacky it gets. What if the player doesn't get fall damage like when he falls in the water or falls on something that is tilted? This means that he will have this "power" stored and when he wants to do it he can just jump off of a building.
I suggest you rework it but this time try having a tracer coming off from the bottom of the player after he starts going down and when the tracer hits something the effect will begin, then use the point the tracer collided with as the middle of the sphere that searches for the players. Not only does this fix most of the problems you have but it also removes accidental exploits and such.
When using the trace will it still have the desired effect? Will it still trigger upon the using impacting the ground. As for using it in water i am not sure whether i would like it to work in water or not but if its best to allow it im fine with that. As for the 'power' being stored i have a timer setup that disables the effect 10 seconds after using it whether thats ideal or not im not sure.
As for the main issue with wonky physics why would it only have an issue with certain players and is currently consistent with how it dosent work.
Note: new issue is that apparently it now ignores where people are and just kills everyone. Could you check the code to see if you can spot the issue.
Thanks again dude you seem to be the only person that has responded useful info thus far.
What I'm saying if that you need to scrap the code and rework it. You can use a tracer to find the position directly under the player which means it's a fixed position that's easy to use as the middle of the sphere. The "power" being stored can also happen if the player doesn't take fall damage at all like if they are crouching inside of an air vent. I'm sorry to say this but that code is a mess and I don't really feel like untangling it, it's way easier to start over. If you have any questions on how to make a new one I can do a little tutorial code.
Could you maybe explain what in the code is a mess. I know from when i was making it being dissatisfied with how the landing was calculated as well as removing the ability after it was used or denying the ability. which i would happily rework but im confused as why i need to scrap the code when the small parts such as the secondary sfx or the actual damage/effects section. id quite happily improve them if i knew what you think is the main issues with them.
As for using the tracer and the main reason why i wanted it to rely on falldamage is that i didnt want it to be used indoors where someone dosent even have to fly in the air and just need the tracer distance to = 0 by the end.
From my previous comment i fixed the issue regarding it killing people without a radius as i didnt realise that the recommend function :OBBCenter() provides local coordinates and so i just added :LocalToWorld() to fix it.
Still unsure about the main issue that this thread was about but it has improved the SWEP generally despite it being less than optimal.
Thanks again.
Bump: this is taking quite a while for something that could be an incredibly easy fix if anyone has anymore information or wants to tell me the issues with the current code it would be super helpful. Thanks
Sorry, you need to Log In to post a reply to this thread.