Unable to edit a Dictionary Value

Hello, today i was working on cartel family system, saving all the data in a dictionary

Here is when i set the Values

print("Creating cartel id:".. CartelID)
	
	Cartel_Info[CartelID] = {
		["ID"] = CartelID,
		["CartelName"] = CartelName,
		["IconID"] = CartelIcon,
		["Date"] = Date,
		["OwnerID"] = Player.UserId,
		["Funds"] = 0,
		["Members"] = 1,
		["BaseCFrame"] = BaseCFrame,
		["Bounty"] = 0
	}

But later on in my code i tried to edit the “Members” Value

local FindCartelInTable = Cartel_Info[id]
FindCartelInTable["Members"] = FindCartelInTable["Members"] + 1

I tried multiple ways and i dont understand what i did wrong :sad:

You are passing by value not by reference

this should work

Cartel_Info[id]["Members"] += 1

EDIT: nvm FindCartelInTable is referencing a table so it should pass by reference

alright, thank you, ill try that

sadly it did not work, i really have no idea what i did wrong

I realized that was not the problem, can I see the whole script

Are there any errors in your output? If there are, please screenshot them (unless the error’s the one in the title of the thread).
I’d also suggest doing FindCartelInTable["Members"] += 1 to shorten your code (also is less to type, and would save you some hassle if you renamed ‘Members’ to something else).

The full script is 200 lines and can get confusing, here is the function


local function leave_join_func(player, id, status)
	local Bools = player:FindFirstChild("Bools")
	local focus_cartel = FamilyFolder:FindFirstChild("cartelid".. id)
	--local FindCartelInTable = Cartel_Info[id]
	local CartelOwnerInst = game.Players:GetPlayerByUserId(focus_cartel.OwnerID.Value)
	local OwnerGui = CartelOwnerInst.PlayerGui
	local NotifAskGui = OwnerGui:FindFirstChild("AskToJoin")
	local Accept = NotifAskGui.Frame.Accept
	local Decline = NotifAskGui.Frame.Decline
	local HeadShot = NotifAskGui.Frame.Headshot
	local Content = NotifAskGui.Frame.Content
	av_headshot(player, HeadShot)
	if status == "J" then
		print(status)
		
		NotifAskGui.Enabled = true
		Content.Text = player.Name.. " would like to join your cartel"
		Accept.MouseButton1Click:Connect(function()
		Content.Text = player.Name.. " has joined your cartel!"
			wait(1)
			NotifAskGui.Enabled = false
			Cartel_Info[id]["Members"] += 1
			print("worked?")
			Bools.IsInCartel.Value = true
			Bools.CartelId.Value = id


			SetInfo()
			UpdateTable()
		end)
		Decline.MouseButton1Click:Connect(function()
			NotifAskGui.Enabled = false
		end)
		
		

	elseif status == "L" then
		print(status)
		Bools.IsInCartel.Value = false
		Bools.CartelId.Value = nil
		SetInfo()
		UpdateTable()
	end
	
	
end

The Error:

  15:20:16.560  ServerScriptService.MainScript:197: attempt to index nil with 'Members'  -  Server - MainScript:197

where is Cartel_Info referenced from here

This simply means the table doesn’t exist. I’d suggest putting something in place to prevent the error from happening, just so nothing breaks.
Example:

local FindCartelInTable = Cartel_Info[id]
if FindCartelInTable then
    FindCartelInTable["Members"] += 1
else
    warn("Cartel", id, "doesn't exist!")
end

That is not their problem, I’m assuming their problem is they think they are making new cartel objects when they really aren’t that’s why I asked for the rest of the script.

Screenshot 2021-08-16 155320

it does exist, thats whats confusing

Cartel_Info is an empty dictionary

Can you send where Cartel_Info is created, I think you are creating a blank table when you do Cartel_Info[id], what you have to do is make it a class (OOP) and assign a “new” function which creates a new cartel when called

The IDs mismatch then, if it is a valid ID yet doesn’t register on that other code snippet. Try printing the ID when you are about to increment the Members value?

local sus_ranks = {
  baka = { "tanjiro", "gojo" }
}

local clone = sus_ranks.baka -- gets content and make a new context
clone[1] = "literal sussy baka" -- changing the "tanjiro" value in the clone to the "new value"

print(sus_ranks.baka) --> { "tanjiro", "gojo" }

Yes, i see what you mean, but the table isnt empty when i try to edit the “Members”

that function allows people to join an already existing cartel. created here:


CreateCartel_Remote.OnServerEvent:Connect(function(Player, CartelName, CartelIcon, Date, BaseCFrame)
	local Bools = Player:FindFirstChild("Bools")
	local CartelID = #Cartel_Info + 1
	print("Creating cartel id:".. CartelID)
	
	Cartel_Info[CartelID] = {
		["ID"] = CartelID,
		["CartelName"] = CartelName,
		["IconID"] = CartelIcon,
		["Date"] = Date,
		["OwnerID"] = Player.UserId,
		["Funds"] = 0,
		["Members"] = 1,
		["BaseCFrame"] = BaseCFrame,
		["Bounty"] = 0
	}
	
	SetInfo()
	UpdateTable()
	new_owner(Player, CartelID)
	wait(0.1)
	deploy_base(BaseCFrame, CartelID)
	update_folder(CartelID)
end)

Oh I see so the the Cartel_Info table is supposed to already exist.

In that case when is the previous function running? By the name it seems it’s running when the player joins. The Cartel might not have been created when the leave_join_func function runs.

1 Like

No, the create function runs when the player clicks “Create Cartel”, then clicks on “Join cartel” and the cartel owner is asked to accept the new player. When the owner accepts, the members go +1 and the script update the datastore

thats basically it

Okay so I recommend printing out Cartel_Info in the leave_join_func and see what that prints, if that prints nil you know either “Cartel_Info” doesn’t exist or you are not accessing it (it might not be accessible to this function, is this all in one script? if not how does leave_join_func get access to the Cartel_Info table and is it really accessing it properly or is it returning nil?)

If Cartel_Info does exist you can print “Cartel_Info[id]” and see if that exists (maybe it’s not creating Cartel_Info[id] like you think it is or it’s using a different id)

P.s. sorry it’s hard to diagnose the problem when I don’t know the full script and how everything works

P.p.s. if you know the name of a member to a dictionary (not a variable) you can access it like
Cartel_Info[id].Members += 1
instead of
Cartel_Info[id]["Members"] += 1
I believe that is the preferred syntax

1 Like