Custom TTT Specal Round

I’ve just returned to GMOD and found an old script I was working on. I was developing a simple script that would divide all players in to detective/traitors to make a deathmatch round. I am not really aware of the normal conventions when creating scripts but this is what I have so far:

This is the server side script:
[LUA]
specialRound = false

hook.Add(“TTTPrepareRound”, “SpecialRoundCheck”, function()
util.AddNetworkString(“DisplaySpecialRound”)
local chance = 25
local x = math.random(100)
if (x <= chance) then
specialRound = true
net.Start(“DisplaySpecialRound”)
net.Broadcast()
end

end)

hook.Add(“TTTBeginRound”, “StopSpecialRoundCheck”, function()
if specialRound == true then
util.AddNetworkString(“StopDisplaySpecialRound”)
specialRound = false
net.Start(“StopDisplaySpecialRound”)
net.Broadcast()

	local traitor = false
	
	for k, v in pairs(player.GetAll()) do
		if traitor == false then
			v:SetRole(ROLE_DETECTIVE)
			traitor = true
		else
			v:SetRole(ROLE_TRAITOR)
			traitor = false
		end
		v:AddCredits(3)
		SendFullStateUpdate()
	end
	
	
end

end)
[/LUA]

I believe this script is fine however I’m not sure if is the most optimized way. Also I’m not certain if I need to call the SendFullStateUpdate() method after each role update, after all role updates or if it’s completely unnecessary.

Below is the client script:

[LUA]
local Display = false

net.Receive(“DisplaySpecialRound”, function()
Display = true
surface.PlaySound( “impulse.wav” )
end)

net.Receive(“StopDisplaySpecialRound”, function()
Display = false
end)

hook.Add(“HUDPaint”, “DisplaySpecialRoundHUD”, function()
if Display == true then
local ourMat = Material( “specialroundttt.png” )
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( ourMat ) – If you use Material, cache it!
surface.DrawTexturedRect( ScrW() * 0.5 - 256, ScrH() * 0.2 - 128, 512, 256 )

end

end)
[/LUA]

I don’t think this is the best way to display the image and sound but it sort of works. Currently I’m facing problems with the sound playing (not really sure what the problem is) and the image always displays during the round preparation but sometimes when the round starts the image is still displayed.

for the sound, is that definitely the direct path to it? PlaySound takes a path directly to the sound in the sound folder, so you’d need to include the directories before it.

looks fine though, i’m not sure if this is the best way but you could always split the hook into another function then use hook.Remove to remove the hook, and hook.Add it when the message is received.