• Find a player using ents.FindInSphere
    5 replies, posted
Hello, I don't know why this code doesn't work: [CODE] hook.Add("PlayerSpawn", "runthefunction", function() playerhome() end) local homepos = Vector ( 453.907349, -183.914825, -12250 ) function playerhome()--is the player at home? timer.Create("loop", 1, 0, function() local possphere = ents.FindInSphere( homepos, 1000 ) for _,playerinhome in pairs(possphere) do if (playerinhome:IsPlayer()) then print("This player isn at home") break else print("this player isn't at home!") break end end end) end [/CODE] But If I go to "homepos" position, in the console I only see, "This playerisn't at home". Why? Thank you!
Well it does what you told it to do, loop every second through each entity in a 1000 unit radius around the homepos position and print("This player isn't at home") if there is a player entity found in there somewhere.
After you print the value you break, this destroys the loop and will stop it checking through the other entities that are returned. So if the first entity that is found within that sphere isn't an entity, then the "This player isn't at home" will be printed and the loop will end. Instead you should consider doing something like this if you want an output even if the player isn't home: [CODE] local homepos = Vector ( 453.907349, -183.914825, -12250 ) function playerhome()--is the player at home? timer.Create("loop", 1, 0, function() local possphere = ents.FindInSphere( homepos, 1000 ) local players = {} for _,playerinhome in pairs(possphere) do if (playerinhome:IsPlayer()) then players[_] = playerinhome end end if( players != {} )then print( "Players are home." ) else print( "Players aren't home." ) end end ) end[/CODE] What this code does is: - Looks for entities within the spherical radius of the point. - If the entity found is a player, it is added to a table. - The table is then checked to see if it is blank. - It then outputs whether or not there are players in the area based upon the table. Edit: Robot is also right, the current method you are using has the prints the wrong way round for what you are doing.
Your code can be simplified into [code]function playerhome() -- is the player at home? timer.Create("loop", 1, 0, function() local playersAtHome = false for _, ply in pairs( ents.FindInSphere( homepos, 1000 ) ) do if ( !ply:IsPlayer() ) then continue end playersAtHome = true //PlayerThinkWhileInHome( ply ) -- Do some action when the player is inside a home end if( playersAtHome )then print( "Players are home." ) else print( "Players aren't home." ) end end ) end[/code]
[QUOTE=Robotboy655;50714396]Your code can be simplified into [code]function playerhome() -- is the player at home? timer.Create("loop", 1, 0, function() local playersAtHome = false for _, ply in pairs( ents.FindInSphere( homepos, 1000 ) ) do if ( !ply:IsPlayer() ) then continue end playersAtHome = true //PlayerThinkWhileInHome( ply ) -- Do some action when the player is inside a home end if( playersAtHome )then print( "Players are home." ) else print( "Players aren't home." ) end end ) end[/code][/QUOTE] I know, I made it a table in case he wanted extra information - including which specific players are in the area.
[QUOTE=Robotboy655;50714232]Well it does what you told it to do, loop every second through each entity in a 1000 unit radius around the homepos position and print("This player isn't at home") if there is a player entity found in there somewhere.[/QUOTE] Yes, i'm sorry this is my error, I edited the code, in the original code there isn't this error! [editline]15th July 2016[/editline] Thank you guys!
Sorry, you need to Log In to post a reply to this thread.