I made a script which i know doesn’t work but it doesn’t say why on the Output can somebody help
local players = game:GetService("Players");
local function insertTag(plrs, tagName)
for index, plr in pairs(plrs) do
if plr:FindFirstChild(tagName) then return end
local tag = Instance.new("StringValue")
tag.Name = tagName
tag.Parent = plr
end
end
local function giveRoles()
local contestants = {};
local odders = {}
local gunman;
local healer;
local hero;
for index, player in pairs(players:GetPlayers()) do
if player then
table.insert(contestants, player)
end
end
gunman = contestants[math.random(#contestants)]
for index, player in pairs(contestants) do
if not player then
table.remove(contestants, index)
end
if player == gunman then
table.remove(contestants, index)
break
end
end
healer = contestants[math.random(#contestants)]
for index, player in pairs(contestants) do
if not player then
table.remove(contestants, index)
end
if player == healer then
table.remove(contestants, index)
break
end
end
hero = contestants[math.random(#contestants)]
for index, player in pairs(contestants) do
if not player then
table.remove(contestants, index)
end
if player == hero then
table.remove(contestants, index)
break
end
end
for index, player in pairs(contestants) do
if not player then table.remove(contestants, index) end
if player then
table.insert(odders, player)
end
end
contestants = {}
insertTag(odders, "Odder")
insertTag({gunman}, "Gunman")
insertTag({healer}, "Healer")
insertTag({hero}, "Hero")
I have looked and tried different scripts but all of them are either for two different roles or three. Its meant to pick four different roles and then at the end I put Tags on all of them so I can trace them if they leave or die so we can end the round.
local players = game:GetService("Players")
local roles = {
"Gunman",
"Healer",
"Hero",
"Odder"
}
local numOfRoles = #roles
local function insertTag(plr, tagName)
local tag = Instance.new("StringValue")
tag.Name = tagName
tag.Parent = plr
end
local function giveRoles()
local contestants = players:GetPlayers()
local numOfContestants = #contestants
for i = 1, numOfRoles-1 do
local roleName= roles[i]
local chosenIndex = math.random(i, numOfContestants)
local chosenPlayer = contestants[chosenIndex]
contestants[chosenIndex] = contestans[i]
insertTag(chosenPlayer, roleName)
end
local lastRole = roles[numOfRoles]
for i = numOfRoles, numOfContestants do
insertTag(contestants[i], lastRole)
end
end
Well I need four different roles I know i didn’t specify before but the first three roles will only be one player each and the other role will be everybody else. And then I need a tag that would trace back to check if they are still alive or if they left the game.
I have edited my code a little now because I realized that I had forgot some things and there was an unnecessary thing. I couldn’t find the reason why your code doesn’t work, but the code I posted is supposed to do the same thing with less code repetition (I’m not sure if it works either).
Anyways, the way my code should now work is that in the first loop, it repeats the same thing for each role: it picks a random index that is between the value of the loop variable i and the number of players. Then it stores the player who is in that index in contestants to a variable. After that it sets the value of chosenIndex in contestants to be another player if chosenIndex ~= i. If the chosen index happens to be equal to i, then the chosen player will be set as the new value of chosenIndex in the table, which means that nothing is actually changed in the table. After the value is set, the chosen player isn’t in the table anymore or it is stored in the index equal to i. This prevents the same player from being chosen in the later iterations of the loop, because i, which is the minimum value for choosing the random index, gets bigger each iteration. And because the player in index i is moved to a higher index if it isn’t the chosen player, no players will be skipped either.
The second loop just gives the last role for the rest of the players. It starts from the index after the last index in the first loop, so that the same players won’t be chosen twice.
This explanation may be difficult to understand, sorry for that, but I’m not sure how to better explain it.
Ok, are the first 3 roles randoms or do players choose it?
Also, if they die, is the role taken away and assigned to another player or do they keep the role?
First three roles are meant to be random, and then when they die the role is taken away and is NOT assigned to somebody else. And then depending on which role has died, I will have the script announce the winner