I’m trying to fix code that will select two players at random to have a “penalty kick”. One being a goalkeeper, the other being the person who shoots the ball.
This is my code:
if Team1Score == Team2Score then
print('Draw')
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team1.Name):SetAttribute('Draws', game.ServerStorage.GameFolder.Standings:FindFirstChild(Team1.Name):GetAttribute('Draws') +1)
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team2.Name):SetAttribute('Draws', game.ServerStorage.GameFolder.Standings:FindFirstChild(Team2.Name):GetAttribute('Draws') +1)
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team1.Name).Value += 1
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team2.Name).Value += 1
repeat
local randomTeam1PlayerShooter
local randomTeam2Goalie
repeat
local pickedplayer = game.Players:GetPlayers()[random:NextInteger(1, #game.Players:GetPlayers())]
if pickedplayer:GetAttribute('TeamName') == Team1.Name then
randomTeam1PlayerShooter = pickedplayer
end
local canfind2 = false
for _,v in pairs(game.Players:GetPlayers()) do
if v:GetAttribute('TeamName') == Team1.Name then
canfind2 = true
end
end
task.wait()
until canfind2 == false or randomTeam1PlayerShooter:GetAttribute('TeamName') == Team1.Name
repeat
local pickedplayer = game.Players:GetPlayers()[random:NextInteger(1, #game.Players:GetPlayers())]
if pickedplayer:GetAttribute('TeamName') == Team2.Name then
randomTeam2Goalie = pickedplayer
end
local canfind2 = false
for _,v in pairs(game.Players:GetPlayers()) do
if v:GetAttribute('TeamName') == Team2.Name then
canfind2 = true
end
end
task.wait()
until canfind2 == false or randomTeam2Goalie:GetAttribute('TeamName') == Team2.Name
if randomTeam2Goalie ~= nil then
randomTeam2Goalie.Character:SetPrimaryPartCFrame(Pitch.PenaltyShots.Team2.GoalieSpawn.CFrame * CFrame.new(0,4,0))
end
if randomTeam1PlayerShooter ~= nil then
randomTeam1PlayerShooter.Character:SetPrimaryPartCFrame(Pitch.PenaltyShots.Team2.ShooterSpawn.CFrame * CFrame.new(0,4,0))
end
Ball = game.ServerStorage.Ball:Clone()
Ball.Parent = workspace
Ball.Position = Pitch.PenaltyShots.Team2.ballSpawnPoint.Position
CurrentTime = 0
repeat
game.ServerStorage.GameFolder.Other.Time.Value = PenaltyTime - CurrentTime
CurrentTime += 1
task.wait(1)
until CurrentTime > PenaltyTime
Ball:Destroy()
However, the problem is that when it tries to select a random player from each team, it gives me the following error:
I never use random:NextInteger and thus don’t remember what it was used for, try to do math.random instead. It is giving you nil when trying to get a player with the number. I also remembered that at times #game.Players:GetPlayers() can be a bit finicky.
Random.NextInteger is a function similar to math.random but with some improvements. For example, the random number is not based on the time the script started, preventing duplicate values. They are mainly interchangeable.
I have experienced this in the past too.
pickedplayer most likely does not exist in this statement. It is defined here:
I recommend printing out the number and the result of the selection, since I can’t really figure out what is wrong here.
Well, I dont know why, but I printed both pickedplayer and randomTeam2Goalie and for some reason the value randomTeam2Goalie would come back as nil even though pickedplayer came back as a PlayerName?
So I just set it equal to pickedplayer again later on right before I that line that kept erroring and it worked. Thanks!
if Team1Score == Team2Score then
print('Draw')
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team1.Name):SetAttribute('Draws', game.ServerStorage.GameFolder.Standings:FindFirstChild(Team1.Name):GetAttribute('Draws') +1)
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team2.Name):SetAttribute('Draws', game.ServerStorage.GameFolder.Standings:FindFirstChild(Team2.Name):GetAttribute('Draws') +1)
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team1.Name).Value += 1
game.ServerStorage.GameFolder.Standings:FindFirstChild(Team2.Name).Value += 1
repeat
local randomTeam1PlayerShooter
local randomTeam2Goalie
repeat
local pickedplayer = game.Players:GetPlayers()[random:NextInteger(1, #game.Players:GetPlayers())]
if pickedplayer:GetAttribute('TeamName') == Team1.Name then
randomTeam1PlayerShooter = pickedplayer
end
local canfind2 = false
for _,v in pairs(game.Players:GetPlayers()) do
if v:GetAttribute('TeamName') == Team1.Name then
canfind2 = true
end
end
print(pickedplayer)
print(randomTeam1PlayerShooter)
randomTeam1PlayerShooter = pickedplayer
print("Set1")
print(randomTeam1PlayerShooter)
task.wait()
until canfind2 == false or randomTeam1PlayerShooter:GetAttribute('TeamName') == Team1.Name
repeat
local pickedplayer = game.Players:GetPlayers()[random:NextInteger(1, #game.Players:GetPlayers())]
if pickedplayer:GetAttribute('TeamName') == Team2.Name then
randomTeam2Goalie = pickedplayer
end
local canfind2 = false
for _,v in pairs(game.Players:GetPlayers()) do
if v:GetAttribute('TeamName') == Team2.Name then
canfind2 = true
end
end
print(pickedplayer)
print(randomTeam2Goalie)
randomTeam2Goalie = pickedplayer
print("Set2")
print(randomTeam2Goalie)
task.wait()
until canfind2 == false or randomTeam2Goalie:GetAttribute('TeamName') == Team2.Name