Hello,
I've been looking through my Murder lua code for some time now, and I can't seem to figure out how to give all players the magnum.
When I figured out which code give's the magnum, I was confused..
sv_rounds.lua
[lua] local noobs = table.Copy(players)
table.RemoveByValue(noobs, murderer)
local magnum = table.Random(noobs)
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
From what I understand (by reading through the Gmod Wiki), "noobs" is copying the table "players", then "murderer" is being searched in the new table "noobs". Now the "magnum" is defined by a random selection of the "murderer". Now the "magnum" is being validated, and once validated, the person who is the "magnum" will receive the magnum weapon.
I cannot express how heavily confused I am with this.
sv_rounds.lua - Code pertaining to the above lua code talking about defining noobs etc.
[lua]
function GM:StartNewRound()
local players = team.GetPlayers(2)
if #players <= 1 then
local ct = ChatText()
ct:Add(translate.minimumPlayers, Color(255, 150, 50))
ct:SendAll()
self:SetRound(0)
return
end
local ct = ChatText()
ct:Add(translate.roundStarted)
ct:SendAll()
self:SetRound(1)
self.RoundUnFreezePlayers = CurTime() + 10
local players = team.GetPlayers(2)
for k,ply in pairs(players) do
ply:UnSpectate()
end
game.CleanUpMap()
self:InitPostEntityAndMapCleanup()
self:ClearAllFootsteps()
local oldMurderer
for k,v in pairs(players) do
if v:GetMurderer() then
oldMurderer = v
end
end
local murderer
// get the weight multiplier
local weightMul = self.MurdererWeight:GetFloat()
// pick a random murderer, weighted
local rand = WeightedRandom()
for k, ply in pairs(players) do
rand:Add(ply.MurdererChance ^ weightMul, ply)
ply.MurdererChance = ply.MurdererChance + 1
end
murderer = rand:Roll()
// allow admins to specify next murderer
if self.ForceNextMurderer && IsValid(self.ForceNextMurderer) && self.ForceNextMurderer:Team() == 2 then
murderer = self.ForceNextMurderer
self.ForceNextMurderer = nil
end
if IsValid(murderer) then
murderer:SetMurderer(true)
end
for k, ply in pairs(players) do
if ply != murderer then
ply:SetMurderer(false)
end
ply:StripWeapons()
ply:KillSilent()
ply:Spawn()
ply:Freeze(true)
local vec = Vector(0, 0, 0)
vec.x = math.Rand(0, 1)
vec.y = math.Rand(0, 1)
vec.z = math.Rand(0, 1)
ply:SetPlayerColor(vec)
ply.LootCollected = 0
ply.HasMoved = false
ply.Frozen = true
ply:SetTKer(false)
ply:CalculateSpeed()
ply:GenerateBystanderName()
end
local noobs = table.Copy(players)
table.RemoveByValue(noobs, murderer)
local magnum = table.Random(noobs)
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
self.MurdererLastKill = CurTime()
hook.Call("OnStartRound")
end
[/lua]
My problem being I don't know how to define players, bystandards with a secret weapon, and the murderer..
Can anyone point me in the right direction?
Noobs is copying the players table. Because the murderer shouldn't spawn with a magnum, he is removed from the noobs table. Magnum is a random player from the noobs table. Then the code checks if the player it chose actually exists, and if they do, it gives them a magnum.
[editline]3rd June 2015[/editline]
To find someone with the secret weapon just do Player:HasWeapon( "weapon_mu_magnum" )
I'm looking at the Gmod Wiki,
Which table.(variable) in the Libraries will allow the selection off all data in that table?
Something like table.All or table.Everything?
-edit--
I figured I would just change the following code..
[lua]
local noobs = table.Copy(players)
table.RemoveByValue(noobs, murderer)
local magnum = table.Random(noobs)
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
to this:
[lua]
local noobs = table.Copy(players)
table.RemoveByValue(noobs, murderer)
local magnum = table.SelectAllData(noobs) -- Whatever would select all the data in that table..
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
Thoughts?
[QUOTE=Consortias;47872853]I'm looking at the Gmod Wiki,
Which table.(variable) in the Libraries will allow the selection off all data in that table?
Something like table.All or table.Everything?
-edit--
I figured I would just change the following code..
[lua]
local noobs = table.Copy(players)
table.RemoveByValue(noobs, murderer)
local magnum = table.Random(noobs)
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
to this:
[lua]
local noobs = table.Copy(players)
table.RemoveByValue(noobs, murderer)
local magnum = table.SelectAllData(noobs) -- Whatever would select all the data in that table..
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
Thoughts?[/QUOTE]
You would use a for loop.
[lua]
for key_name, value in pairs( table ) do
-- where table[key_name] = value
end
[/lua]
unless you want to do something like this:
[lua]
local t = {1,2,3}
local x , y , z = unpack( t )
print( x , y , z )
-- prints 1 2 3
[/lua]
[lua]
for table.noobs, murderer in pairs( noobs ) do
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
Like that?
[QUOTE=Consortias;47875042][lua]
for table.noobs, murderer in pairs( noobs ) do
if IsValid(magnum) then
magnum:Give("weapon_mu_magnum")
end
[/lua]
Like that?[/QUOTE]
No, I don't think that's how it works.
Think a for loop is like a function, the first 2 arguments can be whatever you want, but represent KEY and VALUE (Hence why people commonly use k and v), Value is more frequently used than key. Keys are just basically pointers to a value in a table. If a key is not applied, lua will give an int automatically anyway. Here's a basic table:
[code]
local tbl = { "Pingas!", "More Pingas!", "Overwhelming Bong" } --tbl means table. tbl has 1 table with 3 VALUES
for KEY, VALUE in pairs(tbl) do -- KEY being KEY and VALUE being VALUE (Like previously discussed) and in the brackets, my local table just created
print(VALUE, KEY) -- This will print the VALUE and KEY that it is currently working on
end
//---------------------------
OUTPUT
---------------------------//
Pingas! 1
More Pingas! 2
Overwhelming Bong 3
--This probably wont be in order, if you want it in order, you can use iparis, but that's more complicated and uses more processing time
[/code]
if you want more information on tables in lua, have a look at this useful resource, [url]http://lua-users.org/wiki/TablesTutorial[/url]
Also, make sure to 'end' everything off with end statements otherwise lua will go nuts.
[QUOTE=Exploderguy;47875209]
[code]
local tbl = ["Pingas!", "More Pingas!", "Overwhelming Bong"]
[/code][/QUOTE]
You kinda screwed up here, you used [] instead of {} for table constructor.
[QUOTE=mijyuoon;47877196]You kinda screwed up here, you used [] instead of {} for table constructor.[/QUOTE]
Sorry, I've been playing with JavaScript too much lately.. :(
Sorry, you need to Log In to post a reply to this thread.