How would I make this i,v in pairs function find all the objects in a table and find the object that has “True” and output the name of that object to reference later? I found some posts about the pairs function but doesn’t help me with my specific issue. Any help is appreciated. Example:
local table = boolean1, boolean2, boolean3 (true)
local name
for i,v in pairs (table) do
if instance == false then
end
else
name = (boolean which is true)
end
i
is the number of the items, v
is the items returned after a specified condition
TTable = {}
for i,v in Table do
if v == true then
print(v) -- something like this
table.insert(TTable, v)
end
end
He could be referring to a dictionary, having probably confused tables with dictionaries.
Maybe he needs to find things, or variables I actually really don’t know what to call them, sorry that have the name true in the dictionary, or values in the variable-things with the boolean true, or a string value containing true.
Sorry for pointing out a very unlikely possibility
… and also the very overdone formatting …
Also please use preformatted text for code. They have this </> icon and they look like this:
-- Example code
local isRobloxAwesome = true
if isRobloxAwesome == true then
print("ROBLOX is awesome!")
end
Im pretty sure its still a table, its just whats in them that makes it a dictionary
Yeah, you have a point. Although, still. He could’ve been referring to that.
Quick side note: your code has a typo in the Table variable, and to make it a proper variable I’m pretty sure you have to add a local
prefix to make it right.
I have a lot of scripts that I’m going to change into boolean values soon so I am trying to find the one script/boolean that has enabled/Value ticked to ‘true’ and make the function name that ‘true’ object to reference later
Correct me if I’m wrong, but I’m pretty sure this is the difference?
local Table = {"Apple", "Banana", "Orange"}
local Dictionary = {
Apple = 10,
Banana = 15,
Orange = 7
}
A table is either a dictionary or an array. An array has only number indexes, and dictionaries have more than just number indexes (like words or other values).
Okay, this works thank you. But if one of the objects value is updated while the game server is active and playing it does not name the newly updated object. It only names the object with the true value if the value was true while editing it manually while the game server is inactive.
game.ReplicatedStorage.DoEmote.OnServerEvent:Connect(function(WhoPlayer)
wait(0.2)
local Emotess = WhoPlayer.Character.Emotes:GetChildren()
local TTable = Emotess
for i,v in TTable do
if v.Enabled == true then
print(v.Name)
table.insert(TTable, v.Name)
end
end
end)
@DiamondSwordOfScript I think i found a way to do this:
local TTable = {}
local Dictionary = {
Bool1 = true;
Bool2 = true
}
for i,v in Dictionary do
if v == true then
print(i, v) -- prints both Name and value
TTable[i] = v -- Creates a Variable for the Bool inside the Table for later use
end
end
print(TTable) -- Prints Table
Output:
12:07:06.025 ▼ {
["Bool1"] = true,
["Bool2"] = true
} - Server - Script:20
It turns out in a table, i
is the Name, and v
is the Value
Would I have to input and define all the objects that it searches through in the dictionary? And can’t it just edit a local variable to the name of the boolean value object which is true?
Not Exactly, it looks through every value until it finds the objects with a specified Value under the if
statement, if you want to be real specific on what you want like its Name
, Color
, etc, not sure
I’m not exactly sure what you mean here
I want something to look through a list of true/false values and get the name of that object which is true and set it to a variable. Example:
list of values = "a1 (false), a2(false), a3(true) | a3 is the value that is true
local Name = nil
in pairs (list of values)
if AnObjectItFound == true then
Name = ValueWhichIsTrue
print(Name)
Console: a3
Not a very good example but I hope you understand
You can just Modify my script for that:
for i,v in Dictionary do
if v == true then
print(i)
end
end
I not sure this is possible without using a table since a Variable usually only stores 1 value
The code you’ve provided doesn’t achieve what you’re trying to do. You need to use an if statement to check if the value is true and then if it is, assign the key (the name) to the variable.
local table = {boolean1 = false, boolean2 = false, boolean3 = true}
local name
for i, v in pairs (table) do
if v == true then
name = i
end
end
print(name) -- This will print "boolean3"
Would there be a way to automatically assign the table booleans to a bunch of BoolValue’s and look through all of them?
Sure, it’s possible.
But it depends on how you want it to work. If you want it to only read the values once, (for example, settings you set in the studio and NOT set by scripts) it is very simple
local folder: Instane = script.config -- Instance with BoolValue children
local config: { boolean } = {} -- Table containing BoolValues names and values
for _,bv: BoolValue in pairs(folder:GetChildren()) do
config[bv.Name] = bv.Value
end
print(config.PlayerSpeed) -- Whatever the BoolValue called "PlayerSpeed" is set to
Keep in mind if they are changed by scripts during runtime you’ll have to use another method
I have scripts update the boolean values so I want to make this code print the updated BoolValue objects name
game.ReplicatedStorage.DoEmote.OnServerEvent:Connect(function(WhoPlayer)
wait(0.2)
local Emotess = WhoPlayer.Character.Emotes:GetChildren()
local TTable = Emotess
for i,v in TTable do
if v.Enabled == true then
print(v.Name)
table.insert(TTable, v.Name)
end
end
end)
My issue has been solved. I had to have the localscript talk to a serverscript to update the bool on the server side then make another script actually do the whole function.
Localscript:
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.DoEmote:FireServer(WhoPlayer)
end)
ServerScript1:
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.DoEmoteAgain:FireServer()
end)
ServerScript2:
game.ReplicatedStorage.DoEmoteAgain.OnServerEvent:Connect(function(player)
local Emotes = game.Workspace:WaitForChild(player.Name).Emotes
Emotes:FindFirstChild("a6").Value = true
for i, EmoteObj in pairs(Emotes:GetChildren()) do
if EmoteObj.Name ~= "a6" then
EmoteObj.Value = false
end
end
end)
Script3:
local remoteEvent = game.ReplicatedStorage.DoEmote
local folder = nil
local function checkValues()
while folder == nil do
wait(0.1)
end
for _, child in pairs(folder:GetChildren()) do
if child:IsA("BoolValue") and child.Value == true then
elseif child:IsA("BoolValue") then
end
end
end
game.ReplicatedStorage.DoEmote.OnServerEvent:Connect(function(player, WhoPlayer)
wait(0.1)
folder = game.Workspace:WaitForChild(WhoPlayer.Name).Emotes
print("Folder defined")
checkValues()
end)
while true do
checkValues()
wait(0.1)
end
There is probably a better way to do this but in a more compact way but this is what worked for me.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.