I am trying to code a datastore that saves a player's UserId to a table inside of a table in another players save. However when I try to do so, it tells me that I attempted to call a nil value at line 38 in this script.

local ds = game:GetService("DataStoreService")
local keystore = ds:GetDataStore("Idon'trememberhowtocode")
local rs = game:GetService("ReplicatedStorage")
local GroupCreationEvent = rs:WaitForChild("GroupCreationEvent")
local AgreeEvent = rs:WaitForChild("AgreeEvent")

GroupCreationEvent.OnServerEvent:Connect(function(plr, data)
	local main = plr.UserId
	print(data)
	local player2--< allows this variable to be called anywhere in this function
	if plr ~= data then
			player2 = game.Players:WaitForChild(data)
			local player2Id = player2.UserId
			local RequestGui = game.ReplicatedStorage["Co-opRequest"]:Clone() --< Clones request gui
			RequestGui.Frame.Message.Text = plr.Name .. " Would like to add you to a Co-op" --< Changes gui text based on player requesting
			RequestGui.Parent = player2.PlayerGui --< Moves the new gui to the players screen
			AgreeEvent.OnServerEvent:Connect(function(plr, data) --< This runs when a decision is made
				if plr.UserId == player2Id and data == true then --< This runs if they agree
					local Table = {
					OwnerId = main
					}
					keystore:SetAsync(player2Id, Table)	--< Adds player2 to the main's save file, this means they share money, building space etc
					local ownerdata = keystore:GetAsync(main)
					local members = ownerdata["Members"]
					if members then
						for i = 1, #members do
							local Conflict
							if plr.UserId ~= members[i] then
								local Conflict = true
								i = #members
							end
							if i == #members and Conflict == false then
								Table.Insert(ownerdata.Members, main)
								print("Success!")
							end
						end
					else
						Table.Insert(ownerdata["Members"], main)
					end
					
					
					local Bool =  game.ReplicatedStorage.PlayerData:WaitForChild(plr.Name):WaitForChild("SaveCancel")
					Bool.Value = true
					print("Co-op confirmed")
				else
					print(data .. "Declined your co-op") --< Runs on a disagree
				end
			end)
		end
end)

And this is the actual datastore script

--< Variables!
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("Idon'trememberhowtocode")
local personalstore = ds:GetDataStore("PersonalData")
local ss = game:GetService("ServerStorage")
local StructureDataBase = ss:WaitForChild("SmallBrain")
local rs = game:GetService("ReplicatedStorage")
local ManualSave = rs:WaitForChild("SaveEvent")
--< Events!
game.Players.PlayerAdded:Connect(function(plr)
	local PersonalFolder = Instance.new("Folder")
	PersonalFolder.Name = plr.Name
	PersonalFolder.Parent = rs:WaitForChild("PlayerData")
	local COORDStorage = Instance.new("Folder")
	COORDStorage.Name = "StructureCoordinates"
	COORDStorage.Parent = rs:WaitForChild("PlayerData"):WaitForChild(plr.Name)
	local IntValues = Instance.new("Folder")
	IntValues.Name = "IntvalueStorage"
	IntValues.Parent = rs:WaitForChild("PlayerData"):WaitForChild(plr.Name)
	local b, e = pcall(store.GetAsync, store, plr.UserId)
	local GroupMembers = Instance.new("Folder")
	GroupMembers.Name = plr.UserId
	GroupMembers.Parent = game.ServerStorage.GroupLists
	local Bool = Instance.new("BoolValue")
	Bool.Name = "SaveCancel"
	Bool.Parent = game.ReplicatedStorage.PlayerData:WaitForChild(plr.Name)
	
	if b then
		local data = store:GetAsync(plr.UserId)
		if data.OwnerId ~= nil then
			local data = store:GetAsync(data.OwnerId)
		else
			local TrueData = store:GetAsync(plr.UserId)
			local LoadType = TrueData["Type"]
			local Upgrade = TrueData["Upgrade"]
		end
	end --Add in a data loading event
end)
--< Functions!
local function save(plr)
	local data = store:GetAsync(plr.UserId)
	local X
	if data.OwnerId ~= nil then
		X = data.OwnerId
	end
	local key = X or plr.UserId
	local Bool = game.ReplicatedStorage.PlayerData:WaitForChild(plr.Name):WaitForChild("SaveCancel")
	if Bool.Value == true then
		print(plr.Name .. "'s data was not saved due to disable data protocol")
	else
		local ftype
		local fupgrade
		local FactoryDetails = {
		Type = ftype,
		Upgrade = fupgrade,
		StructureLocation = {},
		Members = {
		0,
		}
		}
		store:SetAsync(key, "Yay")
	end
	print(key)
	
end 

--< Save on Exit
game.Players.PlayerRemoving:Connect(function(plr)
	save(plr)
end)

--< Manual Save Test
ManualSave.OnServerEvent:Connect(function(plr)
	print("Save Test")
	save(plr)
end)
--< Save on server crash
game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		save(player)
	end
end)

Thanks in advance, Wiggles1305

1 comment

0

Lua tables do not inherit functions of it's class, you have to use a baseclass to push attributes: table.insert(tab, attr). You also already have a variable allocating  'ownerdata["Members"]', so there is no need to pass this fully.