Skip to content

Instantly share code, notes, and snippets.

@BadgerCode
Last active August 24, 2019 23:02
Show Gist options
  • Save BadgerCode/58972df2770dd7cd4d72afdc30fc4137 to your computer and use it in GitHub Desktop.
Save BadgerCode/58972df2770dd7cd4d72afdc30fc4137 to your computer and use it in GitHub Desktop.
Useful when developing Garry's Mod TTT addons. Allows anyone to force their TTT role.
-- Adds a console command "forcerole"
-- forcerole traitor your_name
-- forcerole detective your_name
-- forcerole innocent your_name
if CLIENT then
local function AutoComplete(cmd, stringargs)
local args = string.Split(stringargs, " ")
local suggestions = {}
local role = args[2]
if #args == 2 or (role ~= "traitor" and role ~= "detective" and role ~= "innocent") then
table.insert(suggestions, cmd .. " traitor")
table.insert(suggestions, cmd .. " detective")
table.insert(suggestions, cmd .. " innocent")
return suggestions
end
local targetPlayer = args[3]
for k, v in pairs(player.GetAll()) do
local nick = v:Nick()
if string.find(string.lower(nick), targetPlayer) then
table.insert(suggestions, cmd .. " " .. role .. " \"" .. nick .. "\"")
end
end
return suggestions
end
concommand.Add("forcerole", function(ply, cmd, args)
local targetRole = args[1]
local targetPlayerName = args[2]
local targetPlayer = nil
for k, v in pairs(player.GetAll()) do
if (v:Nick() == targetPlayerName) then
targetPlayer = v
break
end
end
if IsValid(targetPlayer) == false then return end
net.Start("tttforcerole")
net.WriteEntity(targetPlayer)
net.WriteString(targetRole)
net.SendToServer()
end, AutoComplete)
end
if SERVER then
local creditsConVar = GetConVar("ttt_credits_starting")
util.AddNetworkString("tttforcerole")
net.Receive("tttforcerole", function(len, ply)
local target_ply = net.ReadEntity()
local target_role = net.ReadString()
local starting_credits = creditsConVar:GetInt()
local role
local role_string
local role_credits
if target_role == "traitor" or target_role == "t" then
role, role_string, role_credits = ROLE_TRAITOR, "traitor", starting_credits
end
if target_role == "detective" or target_role == "d" then
role, role_string, role_credits = ROLE_DETECTIVE, "detective", starting_credits
end
if target_role == "innocent" or target_role == "i" then
role, role_string, role_credits = ROLE_INNOCENT, "innocent", 0
end
local current_role = target_ply:GetRole()
if GetRoundState() == 1 or GetRoundState() == 2 then
ply:ChatPrint("The round has not begun!")
elseif role == nil then
ply:ChatPrint("Invalid role :\"" .. target_role .. "\" specified")
elseif not target_ply:Alive() then
ply:ChatPrint(target_ply:Nick() .. " is dead!")
elseif current_role == role then
ply:ChatPrint(target_ply:Nick() .. " is already " .. role_string)
else
target_ply:ResetEquipment()
RemoveLoadoutWeapons(target_ply)
RemoveBoughtWeapons(target_ply)
target_ply:SetRole(role)
target_ply:SetCredits(role_credits)
SendFullStateUpdate()
GiveLoadoutItems(target_ply)
GiveLoadoutWeapons(target_ply)
end
target_ply:ChatPrint("Your role is now '" .. role_string .. "'")
if (target_ply ~= ply) then
ply:ChatPrint("Set " .. target_ply:Nick() .. "'s role to " .. role_string)
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment