Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't my random killer script work? (1 is not a valid member of Player)

So, once i run the script and let the script try to choose a random player to pick as the murderer, the output says: "1 is not a valid member of Player"

Here is my script:

for _, v in pairs(game.Players:GetChildren()) do
		local randomSelect = math.random(1,#game.Players:GetPlayers())
		if v == v[randomSelect] then
			revealRoleRemote:FireClient(v,true)
		else
			revealRoleRemote:FireClient(v,false)
			end
		end

Please help!

2 answers

first of all use GetPlayers() instead of GetChildren in the pairs

secondly if v == v[randomSelect] then this line is wrong, you need to do v == game.Players:GetPlayers()[randomSelect]

you need to compare the randomly selected player with the player from the for loop what u currently did what look for an somehting called 1 inside the player `

1 comment

0

This answer is partially wrong. I understand you were trying to do, but that is not what he intended.

So the GetPlayers() gets every individual player, and 1 doesn't exist as either a property or child. So you need to change some things. You need to use the index variable of for i, v in pairs loop. Instead of the value.

local revealRoleRemote = game.ReplicatedStorage.RevealRoleRemote --Please include all of your script.
local playerTab = game.Players:GetPlayers() --Variables should be used, if they are used more than once.
for i, v in pairs(playerTab) do
	local randomSelect = math.random(1,#playerTab)
	if i == randomSelect then --If the index is equal to the randomSelect
		revealRoleRemote:FireClient(v,true)
	else
		revealRoleRemote:FireClient(v,false)
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.