Changing The Value Of A Dictionary

Greetings, I’ve attempted to set a specific dictionary value to true, once a player hits a, “leave” button on a UI. Once the player hits the, “leave ____” button, they are teleported out, which works, but the part that doesn’t work has to do with something that I’m doing incorrectly. I am not quite sure, on what to do as, it isn’t working properly, the script either sets all of the values to true, or the wrong values to true. This has been frustrating, and I’ve tried numerous times to edit the value of what it indexes. Any help will do!

Script

Minor Edits Were Made.

local ServerStorage = game:GetService("ServerStorage")
local ModulesFolder = ServerStorage:WaitForChild("Modules")
local ARandomTab = ModulesFolder:WaitForChild("RandomModule")
local ARandomTable = require(ARandomTab)

		for i = 1, #ARandomTable do
			if ARandomTable[i] == false then
				print(ARandomTable[i])
				ARandomTable[i] = true
				print(ARandomTable[i])
			else
				if ARandomTable[i] == true then
					print(ARandomTable[i])
					break
				end
			end
		end

: | :

Module

Minor Edits Were Made.

local RandomVar = {}

RandomVar[1] = true;
RandomVar[2] = true;
RandomVar[3] = true;
RandomVar[4] = true;
RandomVar[5] = true;
RandomVar[6] = true;
RandomVar[7] = true;
RandomVar[8] = true;
RandomVar[9] = true;
RandomVar[10] = true;
RandomVar[11] = true;
RandomVar[12] = true;

return RandomVar
2 Likes

Can you elaborate on which part isn’t working?
Your description is confusing.

1 Like

Updated the script to the current code.

1 Like

Well, the bit that I’ve provided doesn’t seem to work.

1 Like

Please elaborate on “doesn’t work” - what is it meant to do?

What is the error (if any)?

(also happy birthday)

1 Like

Thank you, and also, there are no errors. The script is meant to change the value of a specific dictionary to true. Such as, [1] = true, [2] = true, etc. However, the script isn’t working the way that it was intended to…

1 Like

Perhaps changing the statement to if not ARandomTable[i] then

This will work if it is nil, or false.

1 Like

The values are predefined, but I’ll try that! Thank you for responding!

1 Like

If you want to change the value of a variable in a table, do this:

We have our table, with pre-defined variables in it, like-a-so:

Table = {[1] = false}

We want to change this so we’ll do:

Table[1] = true

1 Like

“ModuleFolder” is defined as, local ModulesFolder = ServerStorage:WaitForChild("Modules").

2 Likes

I am trying to change the specific value of a table, (true / false). Such as, if a player leaves a car, it changes the value, (true / false), of local isSeated1 = true to isSeated1 = false while keeping other values set to True inside of a table / dictionary, (if a player is sitting in them), such as, local isSeated2 = true, etc... Something like that, however, mine keeps on either changing all of the values to false, or the wrong values to false. I just can’t seem to get this to work.

1 Like

Because you;re looping through the table, that’s why it’s changing all of them i’m assuming.

#ARandomTable, will loop through the table using how many items there are in your table.

1 Like

Hmm, so how would I get the specific seat value that the player left on?..

1 Like

I’d set the name of the seat to the exact name of what’s in the table and then do:

Table[Seat.Name] = false, or true, etc.

2 Likes

Hmm, I’ll try that… thank you for the help!

2 Likes

FYI: Instead of having to set every value in your table, you can use a metamethod to do it for you:

local RandomVar = {}
setmetatable(RandomVar, {
    __index = function() return true end
})

return RandomVar

There’s other metamethods for other cases too.

3 Likes

There’s a lack of explanation as to what the problem is despite being asked numerous times. Can’t really discern a problem except for basic practice issues, such as you using both an else and an if statement instead of elseif.

Note that you should also handle truthy cases first.

for i = 1, #ARandomTable do
    if ARandomTable[i] == true then
        print(ARandomTable[i])
        break
    elseif ARandomTable[i] == false then
        print(ARandomTable[i])
        ARandomTable[i] = true
        print(ARandomTable[i])
    end
end

@ReturnBreakEnd I mean you could but I personally wouldn’t. If you don’t have arbitrary values or you know what your table will contain, better to construct it with the elements or size it needs rather than to add later which forces the table to expand and reallocate.

6 Likes

I’ve officially solved the issue, however, it doesn’t involve the use of secondary tables.

1 Like