• Random Weapons
    18 replies, posted
Hi, I want to make players who join cop (yes I'm using *gag* DarkRP!) to get a random handgun, how do I make it pick a random weapon out of a list in the AddExtraTeam code?
Not gonna try and dive into DarkRP code, but here's generally how you do it: [lua] local t = {} -- table "t" would be your list of random weapons in this case ply:Give(t[math.random(1,#t)]); -- ply is your player to give it to[/lua]
ANybody know how to do this in Darkrp?
You would have to modify abit of the sourcecode, so it doesn't error out when it finds its a table in the list of weapons it should give. I think this is more of a request than a question, since you don't seem to know any lua, or you just haven't tried.
[lua] hook.Add("PlayerSpawn","ResetWeapons",function(ply) local default_weps = {"weapon_physcannon","weapon_physgun"} local white_classes = {TEAM_POLICE} timer.Create(ply:UniqueID().."_resetweps", .5, 0, function() if ply:Alive() && RPExtraTeams && RPExtraTeams[ply:Team] && RPExtraTeams[ply:Team].Weapons then if not table.HasValue(black_classes,ply:Team()) then return end ply:StripWeapons() for a,b in pairs(default_weps) do ply:Give(b) end ply:Give(table.Random(RPExtraTeams[ply:Team].Weapons)) timer.Destroy(ply:UniqueID().."_resetweps") end end) end) [/lua] Untested put in init.lua or any serverside file. If it errors just post here and I will fix.
where do I put the possible weapons? Do I do what the first guy said, but this will make it work in darkRP? [editline]6th February 2011[/editline] [QUOTE=Donkie;27900361]I think this is more of a request than a question, since you don't seem to know any lua, or you just haven't tried.[/QUOTE] I thought I was in the request section.
[QUOTE=zzaacckk;27903261][lua] hook.Add("PlayerSpawn","ResetWeapons",function(ply) local default_weps = {"weapon_physcannon","weapon_physgun"} local black_classes = {TEAM_POLICE} timer.Create(ply:UniqueID().."_resetweps", .5, 0, function() if ply:Alive() && RPExtraTeams && RPExtraTeams[ply:Team] && RPExtraTeams[ply:Team].Weapons then if table.HasValue(black_classes,ply:Team()) then return end ply:StripWeapons() for a,b in pairs(default_weps) do ply:Give(b) end ply:Give(table.Random(RPExtraTeams[ply:Team].Weapons)) timer.Destroy(ply:UniqueID().."_resetweps") end end) end) [/lua] Untested put in init.lua or any serverside file. If it errors just post here and I will fix.[/QUOTE] What the hell are you going on about about? OP wants a random handgun to be given to a cop. (examples: p228, fiveseven) Anyways, here's my shot at this. (Commented + Easily Customizable) [lua] hook.Add("PlayerSpawn", "AddRandomHandgun", function(pl) -- PlayerSpawn when the player (re)spawns, do this check. local teamgetting = { "TEAM_POLICE" } -- Customizable, that's why. local handgun = { "weapon_fiveseven", "weapon_p228" } -- You can add weapons easier this way. if table.HasValue(teamgetting, pl:Team()) then -- Check if player spawning is a cp, don't allow other teams to have this! pl:Give(table.Random(handgun)) -- randomly give cop a weapon from above else return -- Don't need this else, I just like it. end -- end the if end) -- end the hook. [/lua] Sorry if I'm wrong, at least I tried. Oh, and it actually doesn't matter where you put this. Technically. (I suggest darkrp\gamemode\init.lua, darkrp\gamemode\main.lua, lua\autorun\server)
I misread his post but either way he could easily modify it to where it worked that way.
[QUOTE=Derek_SM;27906034] [lua] hook.Add("PlayerSpawn", "AddRandomHandgun", function(pl) -- PlayerSpawn when the player (re)spawns, do this check. local teamgetting = { "TEAM_POLICE" } -- Customizable, that's why. local handgun = { "weapon_fiveseven", "weapon_p228" } -- You can add weapons easier this way. if table.HasValue(teamgetting, pl:Team()) then -- Check if player spawning is a cp, don't allow other teams to have this! pl:Give(table.Random(handgun)) -- randomly give cop a weapon from above else return -- Don't need this else, I just like it. end -- end the if end) -- end the hook. [/lua] [/QUOTE] In the teamgetting table you would have TEAM_POLICE not "TEAM_POLICE"
no it should be "" beacuse that's a string, and if you do TEAM_POLICE i would think i was something like [lua] local TEAM_POLICE = 8 [/lua]
[QUOTE=grunewald;27910657]no it should be "" beacuse that's a string, and if you do TEAM_POLICE i would think i was something like [lua] local TEAM_POLICE = 8 [/lua][/QUOTE] No look at my example, it is not a string in the table. You would do TEAM_POLICE.Name = "8" or whatever. Its like Player or ply, you can do player.MyVal = "lol?" etc. [url]http://wiki.garrysmod.com/?title=Team[/url]
Will Dereks work? Or what do I have to do? (If it does that is awesome.)
[lua] hook.Add("PlayerSpawn","PoliceWeps",function(ply) local random_weps = {} random_weps[1]={"P228","weapon_p228"} random_weps[2]={"Five Seven", "weapon_fiveseven"} local white_classes = {TEAM_POLICE} if ply:Alive() && RPExtraTeams && RPExtraTeams[ply:Team] && RPExtraTeams[ply:Team].Weapons then if not table.HasValue(white_classes,ply:Team()) then return end local r = table.Random(random_weps) ply:Give(r) Notify(ply,1,4,"You're department has issued you a "..r[1]) end end) [/lua] Tested and working. Put in any serverside file located in the DarkRP folder(init.lua).
THANK YOU. Also I like the "You're department has issued you a " and it's easy to customize.
"You are department has issued you a..".. What..
Note: it's better that you use the PlayerLoadout hook in order to minimize conflicting scripts.
lua/autorun/server/ [lua] hook.Add( "OnPlayerChangedTeam", "giverandomweapon", function( ply, oldteam, newteam ) local allowedweapons = { "weapon_deagle2", "weapon_p2282", "weapon_fiveseven2", "weapon_pumpshotgun" } if team.GetName(newteam) == TEAM_POLICE then local chosenweapon = table.Random(allowedweapons) ply:Give(chosenweapon) Notify(ply,1,4,"Your department has issued you a "..chosenweapon) end end) [/lua] I'm learning lua, please give me feedback
^ Wont run when the player respawns.
Lol derp -_-, just noticed lol... also yours wont run if respawning after changing classes is off
Sorry, you need to Log In to post a reply to this thread.