We got an amazing T weapon for our server but innocents seem to like getting it used on them. In order to reduce this we'd like to award it to one random Traitor in the start of a round. Now I understand where to put the code for this (autorun) but that's about it. Here's some psuedocode for the concept, any suggestions?
[CODE]
function weaponStartforT( ply )
hasawarded = 0
--Admins--
if ply isTraitor and hasawarded == 0 then
ply:Give("weapon_ttt_superweapon")
hasawarded=1
end
end
hook.Add("PlayerSpawn", "weaponStartforT", weaponStartforT )[/CODE]
[lua]local function GiveSuperWeapon()
local traitors = {}
for _, ply in pairs( player.GetAll() ) do
if ply:GetRole() == ROLE_TRAITOR then table.insert( traitors, ply ) end
end
local luckyguy = table.Random( traitors )
luckyguy:Give( "ttt_superweapon" )
end
hook.Add( "TTTBeginRound", "GiveSuperWeapon", GiveSuperWeapon )[/lua]
I think you can do ply:IsTraitor() too, not sure.
that seems to have worked excellently, I'm going to leave the topic up for a bit in case I find any problems. Thanks!
[editline]8th August 2013[/editline]
I'm getting the following error:
[ERROR] lua/autorun/frenzy.lua:10: attempt to index local 'luckyguy' (a nil value)
1. fn - lua/autorun/frenzy.lua:10
2. Call - addons/ulib/lua/ulib/shared/hook.lua:183
3. RoundStateChange - gamemodes/terrortown/gamemode/cl_init.lua:138
4. Function - gamemodes/terrortown/gamemode/cl_init.lua:214
5. unknown - lua/includes/modules/usermessage.lua:87
otherwise it seems to work
Means that the script didn't find any traitor. No traitor was added to the list "traitors", thus table.Random couldn't find any.
Try this:
[code]local function GiveSuperWeapon()
local traitors = {}
for _, ply in pairs( player.GetAll() ) do
if ply:IsTraitor() then table.insert( traitors, ply ) end
end
local luckyguy = table.Random( traitors )
luckyguy:Give( "ttt_superweapon" )
end
hook.Add( "TTTBeginRound", "GiveSuperWeapon", GiveSuperWeapon )
[/code]
still giving the same error
[code]local luckyguy = table.Random( traitors )
if IsValid( luckyguy ) then
luckyguy:Give( "ttt_superweapon" )
end[/code]
Should probably check to see if the traitor is valid before attempting to give them the weapon.
I'm not familiar with TTT, but are the traitors even determined when this hook is called, or is it after?
[lua]local function GiveSuperWeapon()
local traitors = GetTraitors()
local luckyguy = table.Random( traitors )
luckyguy:Give( "ttt_superweapon" )
end
if SERVER then hook.Add( "TTTBeginRound", "GiveSuperWeapon", GiveSuperWeapon ) end[/lua]
It should have been server side ;)
Sorry, you need to Log In to post a reply to this thread.