• DarkRP location tracking
    12 replies, posted
Hey, I'm very new to Lua and my understanding is still extremely basic - I am trying to learn but am struggling to comprehend how I could do this. I want to write a script that does this: Checks if the players team is TEAM_SECENG If true then if they are in a certain region of the world (region specified using two opposite corner points Then it should pay them X amount every X minutes. If anyone could help me with this idea I would really appreciate it. Thank you. -Tratman
Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Create]timer.Create[/url] to [URL="[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/player/GetAll]player.GetAll[/url]"]loop through all players[/URL] (Hint: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/ipairs]ipairs[/url]) and if [URL="[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Vector/WithinAABox]Vector:WithinAABox[/url]"]they're inside a specific box[/URL] (Hint: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetPos]Entity:GetPos[/url]) and they are in a certain team ([img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Team]Player:Team[/url] == "TEAM_NAME") give them money with [URL="http://wiki.darkrp.com/index.php/Functions/Player/Server/addMoney"]ply:addMoney[/URL] [editline]adf[/editline] Thanks Ralgoman for the missing parts
First you would want to check if the player is in the certain team with [URL="http://wiki.garrysmod.com/page/Player/Team"]ply:Team()[/URL] and then check if the [URL="http://wiki.garrysmod.com/page/team/GetName"]team.GetName()[/URL] is equal to w/e team you want them to be. If that is true then you want to get the position of those players with [URL="http://wiki.garrysmod.com/page/Entity/GetPos"]Entity:GetPos()[/URL] and check if that position is in a box with [URL="http://wiki.garrysmod.com/page/Vector/WithinAABox"]Vector:WithinAABox()[/URL]. If that returns true then you want to give them money with the darkRP function [URL="http://wiki.darkrp.com/index.php/Functions/Player/Server/addMoney"]ply:addMoney()[/URL].
Thanks guys I appreciate this - I'll try and cobble something together :) [editline]27th May 2017[/editline] I have the following code in a file named checker.lua: [url]https://paste.ee/p/SKXeh[/url] ( the formatting has been slightly altered by paste.ee but the "then" is on the previous line. How do I test if this code works? (Although I doubt it does.) And how do I make it run everytime the server starts up? Thanks for the help guys. :) -T
[CODE] local a, b = Vector(1621.121826, 1363.175293, -62.392197), Vector(2407.789307, 1965.849976, -242.307465) OrderVectors( a, b ) local function SecurityMoney() for k, v in ipairs(player.GetAll()) do if v:Team() == TEAM_SECURITY then if v:GetPos():WithinAABox( a, b ) then v:addMoney(250) end end end end [/CODE] you're on the right track
[QUOTE=Bull29;52290933] you're on the right track[/QUOTE] Thanks Bull, that's really helpful. How do I make this run all the time and when the server starts etc? :)
[QUOTE=Tratman;52292604]Thanks Bull, that's really helpful. How do I make this run all the time and when the server starts etc? :)[/QUOTE] You probably don't want it running all the time, that's hella expensive. I'd use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Create]timer.Create[/url] with a 1 second delay
I've tinkered with that code trying to get it to work but this is the best I can come up with and it still throws errors. [Code] local a, b = Vector(1621.121826, 1363.175293, -62.392197), Vector(2407.789307, 1965.849976, -242.307465) local function SecurityMoney() for k, v in ipairs(player.GetAll()) do if v:Team() == TEAM_SECURITY then if v:GetPos():WithinAABox( a, b ) then v:addMoney(250) end end end end timer.Create( "CheckerTimer", 1, 0, function SecurityMoney() ) [/Code] Error: [Code] [ERROR] lua/autorun/server/securitychecker.lua:5: bad argument #4 to 'Create' (function expected, got nil) 1. Create - [C]:-1 2. unknown - lua/autorun/server/securitychecker.lua:5 [/Code] I removed the order vectors because I couldn't see why it was needed as the vector values are already assigned by the variables and its inclusion was creating an extra error. Thanks for the help guys :happy:
replace [B]function SecurityMoney()[/B] with just [B]SecurityMoney[/B]
[QUOTE=JasonMan34;52295422]replace [B]function SecurityMoney()[/B] with just [B]SecurityMoney[/B][/QUOTE] I did what you said JasonMan and it is now throwing this error: [Code] [ERROR] lua/autorun/server/securitychecker.lua:8: '(' expected near 'for' 1. unknown - lua/autorun/server/securitychecker.lua:0 [/Code]
I meant in the timer creation...
Tratman, if you're asking us about such a simple thing and people come here to help you, don't just deny their advice. [QUOTE]I removed the order vectors because I couldn't see why it was needed as the vector values are already assigned by the variables and its inclusion was creating an extra error.[/QUOTE] For WithinAABox Ordering the vector values is crucial and it isn't doing the thing you think it does - It orders the Vectors, so the first one has the smallest values and the second one has the highest ones, which is really important for WithinAABox - Don't remove stuff just because. Finished piece of code should look like this: [code]local a, b = Vector(1621.121826, 1363.175293, -62.392197), Vector(2407.789307, 1965.849976, -242.307465) OrderVectors( a, b ) local function SecurityMoney() for k, v in ipairs(player.GetAll()) do if v:Team() == TEAM_SECURITY then if v:GetPos():WithinAABox( a, b ) then v:addMoney(250) end end end end timer.Create( "CheckerTimer", 1, 0, SecurityMoney ) [/code]
[QUOTE=RaKo;52297229]Tratman, if you're asking us about such a simple thing and people come here to help you, don't just deny their advice. For WithinAABox Ordering the vector values is crucial and it isn't doing the thing you think it does - It orders the Vectors, so the first one has the smallest values and the second one has the highest ones, which is really important for WithinAABox - Don't remove stuff just because. Finished piece of code should look like this: [code]local a, b = Vector(1621.121826, 1363.175293, -62.392197), Vector(2407.789307, 1965.849976, -242.307465) OrderVectors( a, b ) local function SecurityMoney() for k, v in ipairs(player.GetAll()) do if v:Team() == TEAM_SECURITY then if v:GetPos():WithinAABox( a, b ) then v:addMoney(250) end end end end timer.Create( "CheckerTimer", 1, 0, SecurityMoney ) [/code][/QUOTE] Something like this may seem simple to someone who has experience coding. Everyone has to start somewhere and I asked for help from the community as I was struggling with achieving what I wanted - I did my best to write code which used the right variables. I appreciate all the help I was given and I did not ignore the suggestions of Bull29, I used his code however this specific line was creating an error - I did not understand why it was there so I tried removing it which removed the error. Surely anyone can see why I did this. Thank you to everyone for their help and I don't appreciate my attempts at adapting code being demonised and patronised as "simple".
Sorry, you need to Log In to post a reply to this thread.