Need help with dictionary

You can write your topic however you want, but you need to answer these questions:

  1. I have a script:
-- Initialization
local allBlocks = {}
local Parent = workspace.Folder

for _, starterchild in pairs(Parent:GetChildren()) do
    allBlocks[starterchild.Position] = {
        f = 0.21,
        t = starterchild.Name
    }
end

-- BindableEvent listener
script.ManageBlock.Event:Connect(function(position, f, t)
    SetValue(allBlocks, position, {f = f, t = t})
end)

-- Function to set value in the table
function SetValue(tbl, key, value)
    tbl[key] = {}
    tbl[key]["f"] = value.f
    tbl[key]["t"] = value.t
end

game:GetService("RunService").Heartbeat:Connect(function()
    print(allBlocks[position])
end)

  1. the print(allBlocks[position]) doesnā€™t print the updated allBlocks[position] because it doesnā€™t update, I donā€™t know why (it prints the value from ignition)

  2. I asked chat gpt but it is so stupid, also I rewritted my spaghetti code but still doesnā€™t work. I already had some dictionary issues in the pastā€¦ now itā€™s 3am and Iā€™m becoming angry on my laptopā€¦

Thanks you in advance, If u need more information just ask I really need your help.

It will print the same position? Otherwise you cant change the position because that is what you are keying to. You provide a key (i.e. position, so you can find the value, then change the key with the same key!!)

hello, no it doesnā€™t have to print the position but the f and t values (numbers). The position is the key used for the dictionary. To clarify the issue: when I print allBlocks[position] after SetValue in the BindableEvent, it print the updated value, but when after that I print it in the (ā€œRunServiceā€).Heartbeat it prints only the old value. also if I print the value in BindableEvent listener before SetValue it says " attempt to index nil with ā€˜tā€™" or with fā€¦ Iā€™m lost

Sorry for not fully reading your post. When you index a dictionary using [], it indexes it through a object. Try to use . to index instead.

thats exactly the problem. the position is changing, so when you try to access the key it opens a new one. since it does not exist until after the setvalue function (which creates a new index under the new position), it errors.

iow you need to prevent the position of the part from changing.
you can test this by printing the position inside the setvalue and heartbeat functions and im 99% positive it will print different values

also jesus christ you really did just put a print statement in a heartbeat event

1 Like

yes you were right! thank you! (yeah lol I put a print in hearthbeat but I was at the end of my life after trying to figure out this stupid mistake for like 4 hours and didnā€™t find any solution :joy: )

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.