• How would I make it so if a player gets too close to me it kills them?
    12 replies, posted
I'm just messing around with code, and I got the idea to make players die if they get too close to me. So, my question is: How would I make it so if a player gets too close to me it kills them? I've tried this: [CODE] if ents.FindInSphere( ply:GetPos(), 70 ) then Player:Kill() end [/CODE] But that didn't work :P
Try this: [CODE] for k,v in pairs( ents.FindInSphere( ply:GetPos(), 70 ) ) do v:Kill() end [/CODE] ents.FindInSphere returns a table (like [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ents/GetAll]ents.GetAll[/url] does), so you can loop through it like that to use it [editline]26th March 2016[/editline] Note that you'd need to set the ply variable to be yourself, and also you'd probably have to run this in a [URL="https://wiki.garrysmod.com/page/GM/Think"]think[/URL] hook or something so it checks constantly You might also want to check if v is valid, and if it's a player using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/IsValid]Global.IsValid[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/IsPlayer]Entity:IsPlayer[/url] [editline]26th March 2016[/editline] E.G. [CODE] local ply = player.GetBySteamID( YOUR STEAMID ) hook.Add( 'Think', 'KillPlayers', function() for k,v in pairs( ents.FindInSphere( ply:GetPos(), 70 ) ) do if IsValid(v) and v:IsPlayer() and v ~= ply then v:Kill() end end end ) [/CODE]
I know I'm most likely doing something wrong, because it's giving me this error in console: [CODE] [ERROR] addons/im_the_founder/lua/autorun/wolf_is_the_founder.lua:65: attempt to call method 'Kill' (a nil value) 1. fn - addons/im_the_founder/lua/autorun/wolf_is_the_founder.lua:65 2. unknown - addons/ulib/lua/ulib/shared/hook.lua:110 [/CODE]
if client then return end at the top of your file
[QUOTE=Invule;50008323]if client then return end at the top of your file[/QUOTE] Didn't work.
You're trying to call Kill client-sided when its a serverside function. Can we see the latest code again? Or instead just drop it in autorun/server
[CODE] surface.CreateFont( "CV20", { font = "DermaLarge", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name size = 25, weight = 10000, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = true, additive = false, outline = true, } ) function DrawName( ply ) if !ply:Alive() then return end local offset = Vector( 0, 0, 85 ) local ang = LocalPlayer():EyeAngles() local pos = ply:GetPos() + offset + ang:Up() ang:RotateAroundAxis( ang:Forward(), 90 ) ang:RotateAroundAxis( ang:Right(), 90 ) if ply:SteamID() == "STEAM_*:*:********" then cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 ) draw.DrawText( "Yes, I am the real Founder.", "CV20", 2, 2, Color( 24, 255, 75, 255 ), TEXT_ALIGN_CENTER ) cam.End3D2D() end end hook.Add( "PostPlayerDraw", "DrawName", DrawName ) local CircleMat = Material( "SGM/playercircle" ) hook.Add( "PrePlayerDraw", "PlayerRings", function( ply ) local colour = Color(132,50,29,100) local radius = 70 local trace = {} trace.start = ply:GetPos() + Vector(0,0,50) trace.endpos = trace.start + Vector(0,0,-300) trace.filter = ply local tr = util.TraceLine( trace ) if !tr.HitWorld then tr.HitPos = ply:GetPos() end if ply:SteamID() == "STEAM_*:*:********" then render.SetMaterial( CircleMat ) render.DrawQuadEasy( tr.HitPos + tr.HitNormal, tr.HitNormal, radius, radius, colour ) end end ) local ply = player.GetBySteamID( "STEAM_*:*:********" ) hook.Add( 'Think', 'KillPlayers', function() for k,v in pairs( ents.FindInSphere( ply:GetPos(), 70 ) ) do if IsValid(v) and v:IsPlayer() then v:Kill() end end end ) [/CODE] That's all in one file
Right. Drop the code MPan1 gave you into autorun/server/ and the postplayerdraw & preplaydraw in autorun/client/ Your folder structure should look like this: Client : addons/im_the_founder/lua/autorun/client/cl_init.lua Server: addons/im_the_founder/lua/autorun/server/init.lua
No errors this time, but it doesn't kill anyone.
Are you testing it with bots or something? I don't think it would work with bots. If you're testing it with other players, then maybe the problem is that the sphere is too small [editline]26th March 2016[/editline] Also, I doubt your steam ID would be STEAM_*:*:******** Wildcards don't work with strings
I edited out my SteamID (I don't know why) It kills players now, but it also kills me.
Oh yeah, I'm an idiot. Try checking if v ~= ply in the 'then' statement bit so then it won't kill players that are you (I edited the code I posted before so it does that)
Thank you! It works now :D
Sorry, you need to Log In to post a reply to this thread.