Random Player Selection Code - Issues Working

Hello Devs!

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:

It occurs on this line:

if pickedplayer:GetAttribute('TeamName') == Team2.Name then
						randomTeam2Goalie = pickedplayer

Which is under the third repeat.

I’ve checked, and there is a value called “teamname” but it seems it cant detect the player and actually go in and check that value?

Anyone have any idea what I’m doing wrong here?

There are many lines with GetAttribute, can you tell us which one the error is talking about since we don’t have line numbers?

1 Like

Sorry, it is this line:

if pickedplayer:GetAttribute('TeamName') == Team2.Name then
						randomTeam2Goalie = pickedplayer

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.

1 Like

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.

1 Like

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

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