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
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…
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.
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.