String value doesn't change correctly

Hey DevForum! I’m currently trying to make a round system in my game where it picks 2 players [cannot be the same] and puts their name into a StringValue. It work’s for 1 StringValue but for the other one it shows up as “1” when no players in the server are named as “1”. Please take a look.

if game.ReplicatedStorage.temple1.Value == “” and cd.Value == " " and HcOver == true then
Temple = false
local players = {} – Table
for i,v in pairs(game.Teams.Dead:GetPlayers()) do
table.insert(players,v.Name) – insert player names to table
end
local value = math.random(1,#players)
local picked = players[value]

  		local value2 = math.random(1,#players)
  		local picked2 = players[value2]
  		if value == value2 then
  			local x = math.random(1,#players)
  			repeat wait() x = math.random(1,#players) until x~= picked
  			picked2 = x

  	end
  		game.ReplicatedStorage.temple1.Value = picked
  		game.ReplicatedStorage.temple2.Value = picked2
  	Temple = true
  	
  else

The reason for this is you are setting the value of “picked2” to “x” which has been set to a random number between 1 and “#players”.

Probably because in this line if both values are equal

picked2 = x

You set picked2 to be the index of the player you obtained, when it should be

picked2 = players[x]

Although I think it’s better to remove the first chosen index from the table and then get another radom value to prevent issues

if game.ReplicatedStorage.temple1.Value == "" and cd.Value == " " and HcOver == true then
	Temple = false
	local players = {} – Table
	for i,v in pairs(game.Teams.Dead:GetPlayers()) do
		table.insert(players,v.Name) – insert player names to table
	end
	local value = math.random(1,#players)
	local picked = players[value]
	table.remove(players,value)
	local value2 = math.random(1,#players)
	local picked2 = players[value2]
	game.ReplicatedStorage.temple1.Value = picked
	game.ReplicatedStorage.temple2.Value = picked2
	Temple = true
else

Thank you it is appreciated although it now says "invalid argument #2 to ‘random’ (interval is empty), you don’t have to help but if you could can you link a topic covering this?

Are you testing it solo? If so, that’s probably why

I’m using the “Test” tab in studio with 3 players.

Did only 1 person die? Because if so, it’s because of that, there’d need to be at least 2 people dead. Do you still want it to work even if 1 person died?

I just tried with 5 players instead and did 2 rounds again. The error still occurs. Yes, if possible is there a way to make it work if 1 person died.

You can probably do this

if game.ReplicatedStorage.temple1.Value == "" and cd.Value == " " and HcOver == true then
	Temple = false
	local players = {} – Table
	for i,v in pairs(game.Teams.Dead:GetPlayers()) do
		table.insert(players,v.Name) – insert player names to table
	end
	local picked,picked2 = "", ""
	if #players > 0 then
		local value = math.random(1,#players)
		picked = players[value]
		table.remove(players,value)
	end
	if #players > 0 then
		local value2 = math.random(1,#players)
		picked2 = players[value2]
	end
	game.ReplicatedStorage.temple1.Value = picked
	game.ReplicatedStorage.temple2.Value = picked2
	Temple = true
else

Check first time if there are players in the table and set picked, and check it again after wards and if there still are palyers, get one for picked2 and set them in the values. Otherwise, empty strings

1 Like

Thank you so much! I appreciate your help a lot.

1 Like

Anytime! If you have anymore issues don’t be afraid to make another psot!