DarkRP Escape Prison Code

Can anybody tell me if this will work and if not fix it or explain what I should do to make it work.


pos1 = (1,2,3)
pos2 = (1,2,3)
plypos == arrestedply:GetPos() 

function playerArrested( arrestedply, time, actor)
    if arrestedply:IsPlayer() and plypos:WithinAABox( pos1, pos2 ) then
      arrestedply:unArrest()
end

The code is syntactically and functionally incorrect. Can you give a detailed explanation of when you want the code to be run and what it should do?

Its supposed to unarrest a player in a certain area.

You should at least try before just expecting people to spoonfeed you.

First off pos1, and pos2 should not be global, especially with those variable names. They need to be localized and given more unique names.

Second plypos also needs to be local, and won’t even work outside of the function you have defined.

Third your if statement is missing an end, or your function is missing an end. Regardless you need an end.

Finally playerArrested won’t be called upon anything since there is no hook. You’d need to hook into this.

http://wiki.darkrp.com/index.php/Hooks/Server/playerArrested

Your function also should not be global, and needs a more unique name.

I now have this.


local posi1 == (1,2,3)
local posi2 == (1,2,3)
local plyposi == arrestedply:GetPos() 


hook.Add("playerArrested", "isplayerarrested" , function(arrestedply, time ,actor)

if arrestedply:IsPlayer() and plyposi:WithinAABox(•pos1,•pos2•) then
      arrestedply:unArrest()
      
end

I would do it by checking all players in Think every few seconds if they are arrested and outside of the jail bounds.

[editline]19th July 2017[/editline]

I would read this before continuing on your project: https://www.lua.org/manual/5.3/manual.html

Other than implementation, you have syntax issues in your variable declaration.

I would only do it on arrested players and I wouldn’t want the think to be slow enough for it to be noticeable nor fast enough to cause the server to lag

A cooldown of 1 second should be fine then.

What are the syntax issues?


local posi1 == (1,2,3) -- Variable declarations use single-equals, not double
local posi2 == (1,2,3) -- You need to have "Vector" in front of the parenthesis - it's a function call where you are sending in x, y, and z
local plyposi == arrestedply:GetPos() -- arrestedply isn't defined here. You can't cache this outside of the hook


hook.Add("playerArrested", "isplayerarrested" , function(arrestedply, time ,actor)

if arrestedply:IsPlayer() and plyposi:WithinAABox(•pos1,•pos2•) then -- Weird dots here? Might be a Facepunch editor issue
      arrestedply:unArrest()
      
end
-- Missing an end)

Yeah those dots are meant to be spaces

[editline]19th July 2017[/editline]


local pos1 = Vector(1,2,3)
local pos2 = Vector(1,2,3)


hook.Add("playerArrested", "isplayerarrested" , function(arrestedply, time ,actor)

plypos = arrestedply:GetPos() 
    if arrestedply:IsPlayer() and plypos:WithinAABox(•pos1,•pos2•) then
      arrestedply:unArrest()
      
    end
end) 

Honestly you could avoid doing a think hook by just popping a unique timer on the arrest player using the arrest hook, and use it to check every few seconds to see if they’re in bounds. That’s how I’d handle it.

I obviously did something wrong.


----------------     Config     ----------------
local pos1 = Vector( -1416, 142, -131)
local pos2 = Vector( -1371, 53, -56)
----------------     Config     ----------------

function IsThePlyArrested( arrestedply, time, actor)

plypos = arrestedply:GetPos() 
    if arrestedply:IsPlayer() and plypos:WithinAABox( pos1, pos2 ) then
      arrestedply:unArrest()

    end
end)

hook.Add("playerArrested", "isplayerarrested" , IsThePlyArrested)

timer.Create( "playerinbounds", 1, 0, function("arrestedply") )

You’re passing in a string to a function which isn’t right and your function IsThePlyArrested is wrong because you don’t need a ) at the end of it

Also plypos is global which it doesn’t need to be, same with the function

What you would want to do is make it so the timer is created when the player is arrested like this

Also you don’t want the timer to run forever, you only need it to run for the duration of their arrested time. Once they are un-arrested you can remove the timer



----------------     Config     ----------------
local pos1 = Vector( -1416, 142, -131)
local pos2 = Vector( -1371, 53, -56)
----------------     Config     ----------------

local function IsThePlyArrested( arrestedply, time, actor)
  timer.Create( "playerinbounds"..ply:EntIndex(), 1, time, function()
		if !IsValid(arrestedply) then return end
		
		local plypos = arrestedply:GetPos() 
		
		if arrestedply:IsPlayer() and plypos:WithinAABox( pos1, pos2 ) then
			arrestedply:unArrest()
			timer.Remove("playerinbounds"..ply:EntIndex())
		end
   end)
end

hook.Add("playerArrested", "isplayerarrested" , IsThePlyArrested)


An easier (and more efficient) option to do all of this is to make an entity that uses

ENT:StartTouch

You make the entity not draw / invisible and so players can pass through it but so it still calls StartTouch

You would just place the entity in a doorway or something / where ever you would consider them escaping and then when they hit it, unarrest them and set them back to where they were like this



function ENT:StartTouch(ent)
	if ent:IsPlayer() and ent:isArrested() then
		local playerPos = ent:GetPos()
		ent:unArrest()
		timer.Simple(.05,function()
			ent:SetPos(playerPos)
		end)
	end
end


Would I spawn the entity there manualy?

You may spawn entities through Lua.

ents.Create

This isn’t rust. :wink:

Stop trying to write vectors as (#,#,#) :slight_smile:

I don’t play rust im just terrible at Gmod LUA and LUA in general :rollout:

Nope, I meant the programming language LOL.

I… What? Do you even make stuff for Garry’s Mod?

[editline]20th July 2017[/editline]

Vector