Creating a game on Roblox but a problem I'm having is making sure everyone has a target that isn't themself or someone who already targets them.
Say if you had 4 players assigned targets randomly. I want to prevent this outcome.
Player1 targets Player2
Player2 targets Player3
Player3 targets Player1
Player4 targets Player4
or Player3 targets Player4
Player4 targets Player3
function aFunctionfdfsd() local targetList = {} -- List of people(3+) who haven't been targeted while myTarget == Me or myTarget == targetsMe do -- cant be yourself or exchanged ranPlayer = math.random(1, #targetList) myTarget = targetList[ranPlayer] --HELP HERE ? end table.remove(targetList, targetList[ranPlayer]) end
Well, let me help you provide an example, this will not work if you just copy paste but this is for you to understand and learn.
local Players = game:GetService("Players"):GetPlayers() -- So first you'll find a way to get an array of players in game local targetList = {} local function assignTargets() for i = 1, #Players do -- so for every player there is if Players[i+1] ~= nil then -- so if there is a player after this player then targetList[Players[i]] = Players[i+1] -- assign this player's target as that player else -- else if there are no players after this player meaning this player is the LAST player targetList[Players[i]] = Players[1] -- Player 1 will always not be assigned yet because he's first in line so you set the last person to the first player end end end --[[ how this works: so we have: Player1 Player2 Player3 Player4 we go through list and assign player1 to player2, player2 to player3, player3 to player4 and player4 to player1. ]]-- assignTargets()
Also I'm sorry if there are any typing errors, I wrote this on the spot. Anyway, you don't need the Players to be random because when someone leaves the game or joins the list will always change.
Anyway that's how you assign the players a target without any overlaps. Hope this helped! Good luck with what you're up to.
If you really want a Random list make sure to contact me through comments.