Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Iterate through dictionary, with times set on the dictionary itself?

I recently made a program that converts certain files into tables. The table that this program returns has two things. A time, and a string value. (For example it'd be something like [26.5625] = "g",) I'd like to make the script run on the time set by the dictionary/table, and i can then pass the string into a function or something. Is that possible?

An example of a dictionary / table im working with would be:

local test= {
[0.0] = "P",
[0.5] = "h@",
[1.0] = "wY",
[1.5] = "oYE",
[2.0] = "(",
[2.5] = "ygW",
[3.0] = "yrhO",
[3.5] = "g@",
[4.0] = "wY",
[4.5] = "oYE",
[5.0] = "D2",
[5.5] = "wY",
[6.0] = "oYPE",
[6.5] = "h1",
[7.0] = "wu",
[7.5] = "uoSE",
[7.625] = "s",
[7.75] = "a",
[7.875] = "s",
[8.0] = "l8",
[8.5] = "uW",
[9.0] = "uthP",
[9.5] = "J4",
[10.0] = "qT"
}

2 comments

0

This site is NOT a request site, you have to attempt the question first. Try searching the WIKI at https://developer.roblox.com/

0

I have tried it, many times, and i have done research on the dev hub, as far as i know, there's nothing.

1 answer

While a knee-jerk reaction is to use pairs to go through every key value pair the order of enumeration is not guaranteed to be in order. See next. We clearly want each element to be in order so we will have to do some sorting of keys.

The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numerical order, use a numerical for.)

First we will need to fetch keys. We will collect them as elements of a table so that order is guaranteed.

Then we will enumerate over each of the elements. First we will wait the required amount of time. Then use our special function.

-- First we build our keyset.
local keyset = {}
local index = 0
for key,_ in pairs(test) do
	index = index + 1
	keyset[index] = key
end
-- Let's sort it.
keyset = table.sort(keysort)
-- Then we enumerate.
-- I assume you have a special function you want to call. I'll name it foo.
local lasttime = 0 -- We use to track differences in time.
for i = 1,#keyset do
	local currenttime = keyset[i]
	wait(currenttime - lasttime)
	lasttime = currenttime
	foo(test[keyset[i]]) -- We use the key to access the old value.
end

Hopefully this helps with whatever you were trying to do. I think this problem is definitely solvable.

This discussion is preserved as it was. New answers, comments, and votes are closed.