• Getting distance between two players with SWEP
    9 replies, posted
Hey guys needed some help creating a swep that would run a small command if the distance between the two players (self and person you are looking at) is within a certain distance. I've looked around but cant seem to really find much on it. thanks for the help!
[code] self:GetOwner():Distance(player:GetPos()) [/code] [url]https://wiki.garrysmod.com/page/Vector/Distance[/url]
You can use [code] local eye = self.Owner:GetEyeTrace() if !IsValid(eye.Entity) or !eye.Entity:IsPlayer() then return end [/code] in the PrimaryAttack or SecondaryAttack function of the SWEP to get the player. [url]http://wiki.garrysmod.com/page/WEAPON/PrimaryAttack[/url] [url]http://wiki.garrysmod.com/page/WEAPON/SecondaryAttack[/url] To measure the distance between them you can use [code] if self.Owner:GetPos():Distance(eye.HitPos) > MaxDist then return end [/code] [url]http://wiki.garrysmod.com/page/Vector/Distance[/url]
mug_swep.lua:35: attempt to index global 'eye' (a nil value) Edit: Nvm i see the problem now. [editline]15th May 2017[/editline] local eye = self.Owner:GetEyeTrace() if !IsValid(eye.Entity) or !eye.Entity:IsPlayer() then return end if self.Owner:GetPos():Distance(eye.HitPos) > 10 then self.Owner:ConCommand( "say /advert i am mugging you!") else Print("that player is too far away") end Is the code i have now and nothing seems to be happening. Will this work with regular NPC's or does it have to be an actual player?
10 is extremely close, try 100 or 200. If you mean Bots, they should work, NPCs wont.
[QUOTE=Emerghency;52231075] local eye = self.Owner:GetEyeTrace() if !IsValid(eye.Entity) or !eye.Entity:IsPlayer() then return end if self.Owner:GetPos():Distance(eye.HitPos) > 10 then self.Owner:ConCommand( "say /advert i am mugging you!") else Print("that player is too far away") end Is the code i have now and nothing seems to be happening. Will this work with regular NPC's or does it have to be an actual player?[/QUOTE] Where is this located? This would work with players only, that's what you're checking for here: [I]if !IsValid(eye.Entity) or [B]!eye.Entity:IsPlayer()[/B] then return end[/I] Also this would pretty much always print "that player is too far away" because 10 units is less than your player model's girth
local eye = self.Owner:GetEyeTrace() if !IsValid(eye.Entity) or !eye.Entity:IsPlayer() then return end if self.Owner:GetPos():Distance(eye.HitPos) > 25 then self.Owner:ConCommand( "say /advert i am mugging you!") else self.Owner:ConCommand("say That player is too far away.") end for some reason always does the advert no matter how far away from the target i am, also this is in the primary function for a SWEP. Also could it work with a swep to where the player being mugged also gets a small message pop up on there screen basically saying (You are being mugged by ****) Edit: Im stupid and used the wrong sign xD (should be less than, not greater than.) However im still curious could i still have it set up to where after you "mug" the person they get a message on their screen?
You can use [code] eye.Entity:PrintMessage(HUD_PRINTTALK,"Message") [/code] for a chat message or [code] eye.Entity:PrintMessage(HUD_PRINTCENTER,"Message") [/code] for a message in the center of the screen.
2 more questions then im done i promise xD i mean the other player having a message printed, not the one with the SWEP equipped. also, how do i use :PrintMessage to print the players name? its getting a table value and expecting a string.
eye.Entity is the player thats being watched by the player with the SWEP. self.Owner is the player with the SWEP. To get the name you can use [code] player:Name() [/code] [url]http://wiki.garrysmod.com/page/Player/Name[/url] Used in a string: [code] eye.Entity:PrintMessage(HUD_PRINTCENTER,"The name is: "..self.Owner:Name()..".") [/code]
Sorry, you need to Log In to post a reply to this thread.