Error in Assigning a Random Player as a Cloned Model

I am trying to randomly select a player who has died in my game to have a special role. This involves transforming their avatar into a cloned model. However, I cannot seem to do this. This game uses a server-sided script and a module script; these scripts will not be posted in full here but rather just the important parts.

Server Script:

local Round = require(script.RoundModule)

local ChosenAdmin = Round.ChooseAdmin()
Round.DressAdmin(ChosenAdmin)

Module Script:

local module = {}

function module.ChooseAdmin() -- This block works!
	local Players = game:GetService("Players"):GetPlayers()
	local Teams = game:GetService("Teams")
	local RandomObj = Random.new()
	
	local Nominated = {}
	
	for i, Player in pairs(game.Players:GetPlayers()) do
		if Player:FindFirstChild("DiedLastRound") then
			table.insert(Player, Nominated)
		else
			return end
	end
	local ChosenAdmin = Nominated[RandomObj:NextInteger(1, #Nominated)]

	ChosenAdmin.Team = Teams["Agents"]
	return ChosenAdmin
end

function module.DressAdmin(NominatedPlayer) -- This one does not!
	local Character 
	
	Character = game.ReplicatedStorage.Administrator:Clone()
	print("Admin rig identified")

	NominatedPlayer.Character = Character -- attempt to index nil with 'Character'
	Character.Parent = workspace
end

return module

You are trying to add a Nominated table to the player when it should be the other way around (table.insert(Nominated, Player))

There may also be an error here (I don’t know exactly what the script does, so it may not be), but if the table is empty, then you will not get the player

1 Like

I have made the change, but I still have the error. Must be the table, as you mentioned.

What do you recommend I do to make this work?

When using return, your loop stopped, that is, if 1 player does not have this value, then the loop stops and the table

for i, Player in pairs(game.Players:GetPlayers()) do
	if Player:FindFirstChild("DiedLastRound") then
		table.insert(Nominated, Player)
	end
end
1 Like

Alright, that seems to work.

However, the round loop stops as it cannot find a player if no one has died (agents in this case).

Therefore, how would I stop this i loop if the loop itself cannot find any players who have the DiedLastRound stringvalue attached to their player object, but leave it running if there are players who hold this value?

for i, Player in pairs(game.Players:GetPlayers()) do
	if Player:FindFirstChild("DiedLastRound") then
		table.insert(Nominated, Player)
	end
end
if #Nominated==0 then
	return nil --code if player not found
end

Or what?

I get the same error - attempt to index nil with ‘Character’ on the line NorminatedPlayer.Character = Character - and the round loop freezes.

This is what my module script looks like so far:

function module.ChooseAdmin()
	local Players = game:GetService("Players"):GetPlayers()
	local Teams = game:GetService("Teams")
	local RandomObj = Random.new()

	local Nominated = {}

	for i, Player in pairs(game.Players:GetPlayers()) do
		if Player:FindFirstChild("DiedLastRound") then
			table.insert(Nominated, Player)
		end
	end
	
	if #Nominated == 0 then
		return nil
	end
	
	local ChosenAdmin = Nominated[RandomObj:NextInteger(1, #Nominated)]
	
	local String = Instance.new("StringValue")
	String.Name = "Chosen"
	String.Parent = ChosenAdmin

	ChosenAdmin.Team = Teams["Agents"]
	return ChosenAdmin
end

function module.DressAdmin(NominatedPlayer)
	local Character 

	Character = game.ReplicatedStorage.Administrator:Clone()
	print("Admin rig identified")

	NominatedPlayer.Character = Character -- attempt to index nil with 'Character' .. same error as I started with 
	Character.Parent = workspace
end

Perhaps I move the code block you mentioned elsewhere…?

if player not found the ChosenAdmin is nil and you attempt to get character from nil

local Round = require(script.RoundModule)

local ChosenAdmin = Round.ChooseAdmin()
Round.DressAdmin(ChosenAdmin) --problem here (maybe)
local Round = require(script.RoundModule)

local ChosenAdmin = Round.ChooseAdmin()
if ChosenAdmin then
	Round.DressAdmin(ChosenAdmin)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.