Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I check if every player has the same value?

Asked by 5 years ago

Say every player gets a bool value, how would I check if every single one of them has theirs set to true?

3 answers

Log in to vote
1
Answered by 5 years ago

Let's look at your example.

You state that you want to check if every player has a BoolValue set to true (assumably within their player instance).

We can do this multiple ways, but let's start simple.

local SameValue = true

for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
    if Player:FindFirstChild("BoolValue").Value == false then
        SameValue = false
    end
end

This code will essentially iterate each player inside of Players, and if any of them have the value as false - SameValue will become false. This will provide you whether all players have a BoolValue set to true (because if after running this for loop SameValue is stated as false, then at least one value is not true).

We could also make it a bit more complicated and applicable by genuinely checking to see if all players have the same value.

local CurrentVals = {}
local SameValue = true

for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
    CurrentVals[#CurrentVals + 1] = Player:FindFirstChild("BoolValue").Value
end

local SelectedValue = Random.new():NextInteger(1, #CurrentVals)
for _,TableIndex in pairs(CurrentVals) do
    if TableIndex ~= CurrentVals[SelectedValue] then
        SameValue = false
    end
end

The code above will start by creating a table of all player's values. It will then choose a random index from the table, and compare it to every value in the table. If at any junction the randomly selected table index is not the same as some table index - SameValue will become false - meaning that every value in the table is not the same.

(there may be some syntax mistakes since I wrote this relatively fast without testing - but hopefully you get an idea of what I mean)

Ad
Log in to vote
0
Answered by
Shadrz 40
5 years ago

This code might be inefficient but it was the best I could come up with.

local Players = game.Players:GetPlayers()
local TrueValues = 0

for _, Players in pairs(game.Players:GetPlayers()) do
    if Player:FindFirstChild("BoolValue") then
        local BoolValue = Player:FindFirstChild("BoolValue")
        if BoolValue.Value == true then
            TrueValues = TrueValues + 1
        end
    end
end

do -- You can remove this if this code is being executed (Along with the end)
    if TrueValues == #Players then
        print('All values are true')
    end
end -- This end
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago
for _,v in pairs(game.Players:GetPlayers()) do
   if v[ "My Bool Value" ].Value == true then
    print':D'
end
end
0
Heads up - you accidently typed :GetPlayers()() instead of :GetPlayers(). Also - provide an explanation for your answer, don't just give code. SummerEquinox 643 — 5y
0
You also omitted an extra portion of code where you actually check if all of the values are the same - this just checks if each player has the true Bool value. SummerEquinox 643 — 5y
0
Hi, I was looking for a way too check if every player has the same value not individual players. ScriptGuilder 2 — 5y
0
R.I.P namespace25 594 — 5y

Answer this question