i want to be able to remove and add ’ Key1 ’ or ’ Key2 ’ from their respective player table using code but keep getting errors / new variable over writing the entire thing.
I’ve tried to do the following
Table[Player.UserId][Key] = Duration
but this results in an error saying Key doesn’t exist, yet I’m trying to create said Key w/ Duration and whatever other variables under Player.UserId without interrupting / changing or removing any other Keys / Variables under Player.UserId.
I should mention that isn’t the only solution I’ve tried and I’ve been unable to find any resources online that are able to help with my issue. My code is as follows
( Module )
function DebounceHandler:Create(Table, Player, Key, Duration)
if Player and Table and Key and Duration then
Table[Player.UserId][Key] = Duration
end
Make sure you’re using the correct operator. Brackets operator [] are for using a variable/a different value as the key. Dot operator . is when you index for exactly what you wrote.
Table[Player.UserId][Key]
--is not the same as
Table[Player.UserId].Key
Yea so I’ve tried something that I believe is similar
function DebounceHandler:Create(Table, Player, Key, Duration)
if Player and Table and Key and Duration then
if Table[Player.UserId] then
Table[Player.UserId]["Stats"] = {Key = Duration}
else
Table[Player.UserId] = {["Stats"] = {}}
Table[Player.UserId]["Stats"] = {Key = Duration}
print("Table Created! : did not exist")
end
end
end
So if I understand correctly you mean use . as if I’m indexing for exactly what I wrote? Because I’m trying to use a variable ( Key ) which could be ANY string but I’l try this out now
This makes a lot of sense, though I think I tried something similar but must’ve still continued to do my original method in another part of my code. I’ve fixed the issue now. Thanks to everyone who responded quickly!