Hi, I am trying to make a system which will return the name of something from a table. Basically I want to check the table to see which name has a value of 1 from the list.
This is the table. (Ignore the fact all values are 0, It updates in game when something happens)
I would like to be able to check which one has a positive value for example if Godly = 1 I would like it to return the name Godly.
Folder1 = {
Godly = 0,
Eternal = 0,
Unique = 0,
Omega = 0,
Demonic = 0,
Zeus = 0,
Artemis = 0,
Frigg = 0,
Loki = 0,
Thor = 0,
Unobtainable = 0,
Impossible = 0,
Broken = 0,
Glitch = 0
}
1 Like
local function findPositiveValue()
for key, value in Folder1 do
if value > 0 then
return key, value
end
end
local itemName, count = findPositiveValue()
Assuming you only need to find the first value that is positive.
Yes, there would only be 1 positive value at a time, I will test this
So im trying to make it work when the player joins the server and for now im just trying to print the value so I can see it but it doesnt seem to have any result.
Players.PlayerAdded:Connect(function(Player)
local function findPositiveValue()
for key, value in ServerData[Player].Folder1 do
if value > 0 then
end
end
local itemName, count = findPositiveValue()
print(itemName)
end
end)
(only difference is the ServerData bit but thats just where the folder is stored)
local function getFirstNaturalKey(dictionary: {}): string
for key, number in dictionary do
if number > 0 then
return key
end
end
end
local function getPlayerDataAsync(player: Player)
local data
while true do
data = ServerData[player]
if not data then
task.wait()
end
end
return data
end
local function onPlayerAdded(player: Player)
-- There is a very good chance that the player's session data has not been set at this time.
-- Consider handling this after setting the data rather than in a new event handler.
local data = getPlayerDataAsync(player)
local name = getFirstNaturalKey(data.Folder1)
print(name)
end
for _, player in Players:GetPlayers()
task.spawn(onPlayerAdded, player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Move this part out of the function to the end of the connection (between the two end
s):
local itemName, count = findPositiveValue()
print(itemName)
And add return key, value
to this condition:
if value > 0 then
end
Also make sure that Players
is defined as game.Players
. If not, replace Players.PlayerAdded
with game.Players.PlayerAdded
.
It should look like this:
game.Players.PlayerAdded:Connect(function(Player)
local function findPositiveValue()
for key, value in ServerData[Player].Folder1 do
if value > 0 then
return key, value
end
end
end
local itemName, count = findPositiveValue()
print(itemName)
end)