Assign one random player to kill another random throughout all players?

If my title sounds confusing, here’s my concept: Each player has another player displayed on their HUD. The goal is for them to kill this player while not getting killed them-self. I’m stuck on actually assigning who kills who.

Here is what I’ve got so far, however this just sets playing[k].killing to a nil value:



function SetKilling()
    playing = shuffleArray(playing)
    
    for k, v in pairs(playing) do
        playing[k].killing = playing[k+1]
    end
end


I’ve checked, and playing contains what it should. If you do need to see any more code, just say and I’ll edit this post and put it in.



	for k, v in pairs( playing ) do
               if ( v.Target ) then return end // might not work, idk
		v.Target = table.Random( playing ) // or v.killing like you did, v.Target is better in my opinion
	end


then to check for their target…



draw.DrawText( "Your target: " .. LocalPlayer().Target, "Default", ScrW() / 2, ScrH() / 2, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )


Sorry, where abouts do I put this? Is this instead of mine? If so, does it guarantee that no two results will be the same? Also probably worth noting the code I posted was server-side.

im not the best at serverside coding but this should work. completely forgot you need it to work clientside also

someone feel free to correct me if im incorrect



	function SetKilling()
		playing = shuffleArray(playing)
		
		for k, v in pairs(playing) do
			v:SetNetworkedString( "Target", table.Random( playing ) )
		end
	end


then use LocalPlayer():GetNetworkedString( “Target” ) for the hud

or use net library

Wait, if I just set and get a network string, is that all the networking I’d need to do?

[editline]8th June 2014[/editline]

It’s working now, thanks!

[editline]8th June 2014[/editline]

Almost working - the table.Random can call repeats when not many players are on, and I don’t want any repeats or for the player to be assigned to himself. How can I stop this?

[editline]8th June 2014[/editline]

Bump. I still need help!

I was doing something similar to this in a gamemode.


local targets = player.GetAll()
		table.Shuffle(targets)
		for i=1, #targets do
			if i == #targets then
				targets*:AssignTarget(targets[1])
				targets*:SetTeam(Assassin.Config.Teams.Playing)
			else
				targets*:AssignTarget(targets[i+1])
			end
		end

table.Shuffle is the table shuffling util I pulled out of TTT,



local rand = math.random
function table.Shuffle(t)
  math.randomseed(os.time())
  local n = #t

  while n > 2 do
    -- n is now the last pertinent index
    local k = rand(n) -- 1 <= k <= n
    -- Quick swap
    t[n], t[k] = t[k], t[n]
    n = n - 1
  end

  return t
end


if ( v == LocalPlayer() ) then return end