In the if statement (of your original code) you need to add a break under Check = false else it will keep running, also use task.wait(), instead of wait().
This is caused by the loop running infinitely in cases where the loop does not meet the requirements that you set for it to end. In your code you have only give math.random 1 argument when it requires 2 (min,max) which is most likely the cause of it never picking a valid player. Try this (hopefully improved) snippet and tell me if it works.
if RandomPlayer1 == RandomPlayer2 then
print("Oops")
print(PickablePlayers)
table.remove(PickablePlayers,2,RandomPlayer2)
repeat
RandomPlayer2 = Players[math.random(1,#PickablePlayers)]
task.wait(.2)
until RandomPlayer1 ~= RandomPlayer2
end
That is incorrect usage of table.remove(). To remove element from an array, first you need to find it. table.remove accepts only 2 arguments, and the third one will be ignored.
To remove RandomPlayer2 from the array use:
Whatever Player array is, I am pretty sure indexes will not match with the PickablePlayers array, especially after table.remove()
I think it should rather be written this way:
It appears my code snippet didn’t work as I didn’t spot the error with table.remove, however I told you that the reason that line is incorrect is because math.random requires a minimum and a maximum argument where you have only given it 1. Replacing it with RandomPlayer2=PickablePlayers[math.random(1,#PickablePlayers)]
Should work
Not sure about the problem with it, can you post possibly a larger snippet of your code? also try testing this with 3-4 players rather then 2 if you haven’t already.
elseif ChosenEvent == "Event9" then
local Players = game.Players:GetPlayers()
local CountValue = Instance.new("IntValue",game.Workspace)
local Number = 0
for i, Count in pairs(Players) do
local Number = Number + 1
CountValue.Value = Number
end
wait(0.2)
if CountValue.Value >= 1 or CountValue.Value == 2 then
print("2playersingame")
local PickablePlayers = {game.Players:GetPlayers()}
local Players = game.Players:GetChildren()
local RandomPlayer1 = Players[math.random(#Players)]
local RandomPlayer2 = Players[math.random(#Players)]
if RandomPlayer1 == RandomPlayer2 then
print("Oops")
print(PickablePlayers)
RandomPlayer2 = PickablePlayers[math.random(1,#PickablePlayers)]
repeat
RandomPlayer2 = PickablePlayers[math.random(1,#PickablePlayers)]
task.wait(.2)
print(RandomPlayer1,RandomPlayer2)
until RandomPlayer1 ~= RandomPlayer2
end
local P1Char = RandomPlayer1.Character:GetChildren()
local P2Char = RandomPlayer2.Character:GetChildren()
print(RandomPlayer1.Name)
print(RandomPlayer2.Name)
if P1Char and P2Char then
for i, Part in pairs(P1Char) do
if Part:IsA("BasePart") then
Part.Transparency = 1
end
if Part:IsA("Accessory") then
Part:Destroy()
end
end
for i, Part in pairs(P2Char) do
if Part:IsA("BasePart") then
Part.Transparency = 1
end
if Part:IsA("Accessory") then
Part:Destroy()
end
end
wait(3)
for _, Part in pairs(P1Char) do
if Part:IsA("BasePart") then
Part.Transparency = 0
end
if Part.Name == "HumanoidRootPart" then
Part.Transparency = 1
end
end
for _, Part in pairs(P2Char) do
if Part:IsA("BasePart") then
Part.Transparency = 0
end
if Part.Name == "HumanoidRootPart" then
Part.Transparency = 1
end
end
end
else
print("Only 1 player in game")
local RandomPlayer1 = Players[math.random(#Players)]
local Char = RandomPlayer1.Character
if Char then
for i, Part in pairs(Char:GetChildren()) do
if Part:IsA("BasePart") then
Part.Transparency = 1
end
if Part:IsA("Accessory") then
Part:Destroy()
end
end
wait(5)
for i, Part in pairs(Char:GetChildren()) do
if Part:IsA("BasePart") then
Part.Transparency = 0
end
if Part.Name == "HumanoidRootPart" then
Part.Transparency = 1
end
end
CountValue:Destroy()
(Testing with more players doesn’t make a difference)
I’ll read it line by line and spot errors. if CountValue.Value >= 1 or CountValue.Value == 2 then, this line can return true if there is 1 OR more players. Replace it with If CountValue.Value > 1 then
local RandomPlayer1 = Players[math.random(#Players)] math.random requires 2 arguments, a minimum and maximum value. Replace with local RandomPlayer1 = Players[math.random(1,#Players)]
local RandomPlayer2 = Players[math.random(#Players)] same issue
print("Only 1 player in game")
local RandomPlayer1 = Players[math.random(#Players)]
same issue
Also consider replacing all of your instances of wait() with the new improved task.wait()
Turns out due to the local PickablePlayers = {game.Players:GetPlayers()} line RandomPlayer1 and 2 where being set to table values. The :GetPlayers() function already returns a table so no need to add extra brackets around em! no clue why my pea brain didnt spot this. Here is the working code
local Players = game.Players:GetPlayers()
local CountValue = Instance.new("IntValue")
CountValue.Parent = workspace
for i, Count in pairs(Players) do
CountValue.Value = i
end
if CountValue.Value > 1 then
print("2playersingame")
local PickablePlayers = game.Players:GetPlayers()
local RandomPlayer1 = PickablePlayers[math.random(1,#PickablePlayers)]
local RandomPlayer2 = PickablePlayers[math.random(1,#PickablePlayers)]
if RandomPlayer1 == RandomPlayer2 then
print("Oops")
repeat
RandomPlayer2 = PickablePlayers[math.random(1,#PickablePlayers)]
task.wait(.2)
print(RandomPlayer1,RandomPlayer2)
until RandomPlayer1 ~= RandomPlayer2
end
local P1Char = RandomPlayer1.Character:GetChildren()
local P2Char = RandomPlayer2.Character:GetChildren()
print(RandomPlayer1.Name,RandomPlayer2.Name)
if P1Char and P2Char then
print("Running code")
for i, Part in pairs(P1Char) do
if Part:IsA("BasePart") then
Part.Transparency = 1
end
if Part:IsA("Accessory") then
Part:Destroy()
end
end
for i, Part in pairs(P2Char) do
if Part:IsA("BasePart") then
Part.Transparency = 1
end
if Part:IsA("Accessory") then
Part:Destroy()
end
end
task.wait(3)
for _, Part in pairs(P1Char) do
if Part:IsA("BasePart") then
Part.Transparency = 0
end
if Part.Name == "HumanoidRootPart" then
Part.Transparency = 1
end
end
for _, Part in pairs(P2Char) do
if Part:IsA("BasePart") then
Part.Transparency = 0
end
if Part.Name == "HumanoidRootPart" then
Part.Transparency = 1
end
end
end
end