TTT ULX Commands - Slay Next Round Glitch

I’m having a problem with the Slay Next Round command- when a player is designated to be slain the next round and the map changes, they are still slain on the first round of the new map however they are still shown as an alive Terrorist. If it’s any consolation what I think may be causing this glitch is the extremely small lag spike that happens when the round begins. I’ve seen this problem on other servers too. It’s a pain to have to force-spectate the player then unforce-spectate them so that they won’t have any impact on the game. Can anyone help out?

Link to addon github: https://github.com/bender180/TTTULX
Link to LUA file: https://github.com/bender180/TTTULX/blob/master/addons/Trouble%20in%20Terrorist%20Town%20ULX%20Commands%20V2/lua/ulx/modules/sh/ttt_admin.lua

Slay Next Round Command (ttt_admin.lua)


function ulx.slaynr( calling_ply, target_ply, num_slay, should_slaynr )
	if not GetConVarString("gamemode") == "terrortown" then ULib.tsayError( calling_ply, gamemode_error, true ) else
		local affected_plys = {}
		local slays_left = tonumber(target_ply:GetPData("slaynr_slays")) or 0
		local current_slay
		local new_slay


		if ulx.getExclusive( target_ply, calling_ply ) then
			ULib.tsayError( calling_ply, ulx.getExclusive( target_ply, calling_ply ), true )
		elseif num_slay < 0 then
			ULib.tsayError( calling_ply, "Invalid integer:\"" .. num_slay .. "\" specified.", true )
		else
			current_slay = tonumber(target_ply:GetPData("slaynr_slays")) or 0
			if not should_slaynr then 	
                new_slay = current_slay + num_slay  
            else
                new_slay = current_slay - num_slay	
            end

            --local slay_reason = reason
            --if slay_reason == "reason" then
            --	slay_reason = false
            --end

			if new_slay > 0 then 		
                target_ply:SetPData("slaynr_slays", new_slay)
                --target_ply:SetPData("slaynr_reason", slay_reason) 
            else
				target_ply:RemovePData("slaynr_slays")
                --target_ply:RemovePData("slaynr_reason")   
            end

	    	local slays_left 	= tonumber(target_ply:GetPData("slaynr_slays"))  or 0
			local slays_removed = ( current_slay - slays_left ) 		or 0

			if slays_removed==0 then
				chat_message = ("#T will not be slain next round.")
			elseif slays_removed > 0 then
				chat_message = ("#A removed ".. slays_removed .." round(s) of slaying from #T.")
			elseif slays_left == 1 then
				chat_message = ("#A will slay #T next round.")
			elseif slays_left > 1 then
				chat_message = ("#A will slay #T for the next ".. tostring(slays_left) .." rounds.")
			end
			ulx.fancyLogAdmin( calling_ply, chat_message, target_ply, reason )
		end
	end
end
local slaynr = ulx.command( CATEGORY_NAME, "ulx slaynr", ulx.slaynr, "!slaynr" )
slaynr:addParam{ type=ULib.cmds.PlayerArg }
slaynr:addParam{ type=ULib.cmds.NumArg, max=100, default=1, hint="rounds", ULib.cmds.optional, ULib.cmds.round }
--slaynr:addParam{ type=ULib.cmds.StringArg, hint="reason",  ULib.cmds.optional}
slaynr:addParam{ type=ULib.cmds.BoolArg, invisible=true }
slaynr:defaultAccess( ULib.ACCESS_ADMIN )
slaynr:help( "Slays target(s) for a number of rounds" )
slaynr:setOpposite( "ulx rslaynr", {_, _, _, true}, "!rslaynr" )
--[Helper Functions]---------------------------------------------------------------------------
hook.Add("TTTBeginRound", "SlayPlayersNextRound", function()
	local affected_plys = {}

	for _,v in pairs(player.GetAll()) do
		local slays_left = tonumber(v:GetPData("slaynr_slays")) or 0
        
		if v:Alive() and slays_left > 0 then
			local slays_left=slays_left -1

			if slays_left == 0 then	
                v:RemovePData("slaynr_slays")
                v:RemovePData("slaynr_reason")
			else 					
                v:SetPData("slaynr_slays", slays_left) 
            end

			v:Kill()
			table.insert( affected_plys, v )
			local corpse = corpse_find(v)
			if corpse then
				corpse_identify(corpse)
				corpse_remove(corpse)
			end
		end
	end

	local slay_message
	for i=1, #affected_plys do
		local v = affected_plys[ i ]
		local string_inbetween

		if i > 1 and #affected_plys == i then
			string_inbetween=" and "
		elseif i > 1 then
			string_inbetween=", "
		end

		string_inbetween = string_inbetween or ""
		slay_message = ( ( slay_message or "") .. string_inbetween )
		slay_message = ( ( slay_message or "") .. v:Nick() )
	end

	local slay_message_context
	if #affected_plys == 1 then slay_message_context ="was" else slay_message_context ="were" end
	if #affected_plys ~= 0 then
		ULib.tsay(_, slay_message .. " ".. slay_message_context .." slain.")
	end
end)

hook.Add("PlayerSpawn", "Inform" , function(ply)
	local slays_left = tonumber(ply:GetPData("slaynr_slays")) or 0
	local slay_reason = false
        
	if ply:Alive() and slays_left > 0 then
		local chat_message = ""

		if slays_left > 0 then
			chat_message = (chat_message .. "You will be slain this round")
		end
		if slays_left > 1 then
			chat_message = (chat_message .. " and ".. (slays_left - 1) .." round(s) after the current round")
		end
		if slay_reason then
			chat_message = (chat_message .. " for \"".. slays_reason .."\".")
		else
			chat_message = (chat_message .. ".")
		end
		ply:ChatPrint(chat_message)
	end
end)

It’s most likely an issue with how TTT handles deaths; the code you posted looks fine. What you should do is store the name of the map when an admin runs the command. If the map name is different when the SlayPlayersNextRound hook is run, kill them, move them to the spectator team, and then move them back.

First off: that lag spikes always occurs. It isn’t specific to your issue.

Second: This occurs because of an error in the PlayerDeath hook,

Got a coder on codehire who created a workaround so it works properly; If anyone wants it they can get it here:
https://www.dropbox.com/s/m2lwesl9bvtbau0/ttt_admin.lua