How to clone this data inside of a table

Greetings, basically I want to clone this data inside of the table, without having to clone the actual table.

local Serverdata = {
	
	["Ward"] = {
		["TestWard"] = {
			["JobId"] = nil,
			["Nurses"] = nil,
			["Patients"] = nil
		}
	},
	
	
	
}

So TestWard and the inside of it is what I need to clone, how would I do this?

game.Players.PlayerAdded:Connect(function(plr)
	if not table.find(Serverdata.Ward, ServerName) then
		local jobid = game.JobId
		local Nurses = 0
		local Patients = 0
		
		for i, v in pairs(game.Players:GetPlayers()) do
			if v:GetRankInGroup(5562794) >= 50 then
				Nurses = i
			else
				Patients = i
			end
		end
             -- CLONE STUFF GOES HERE, EXCEPT IM NOT SURE HOW TO DO IT.

	end
end)

I am still experiencing issues with this, still have no luck or answers.

1 Like
for i, v in pairs(game.Players:GetPlayers()) do
			if v:GetRankInGroup(5562794) >= 50 then
				Nurses = i
			else
				Patients = i
			end
		end

You’re basically saying if the players rank in the group is greater or equal to 50 then you say Nurses = i. If you do that it’ll be 5 for example even if only one of the players is in the group with a group rank of 50 or greater unless you want it to be like that?

and to clone the stuff you need to create a table like so

local function deepCopy(original)
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end
local Clone = deepCopy(TableName)
Clone.JobId = jobid
Clone.Nurses = Nurses
Patients.Patients = Patients
1 Like
local function deepCopy(original)
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end
1 Like

Nurses and Patients are different inside of the game, I’m trying to get a list of nurses (number) and patients (number) inside of the game, being able to return that data with a remote function is what I’m trying to achieve here.

Inside of my client script, which uses this method to get every server inside of that table does not work as seen here.

function reloadServers()
	for _, v in pairs(script.Parent.Frame.UIFrame.Content.Servers.Frame.Content:GetChildren()) do
		if v:IsA("Frame") then
			v:Destroy()

			-- REPLACING FRAMES
			local returnedTable = game.ReplicatedStorage.Events.ServerList:InvokeServer(game.Players.LocalPlayer)
			for i, v in pairs(returnedTable) do
				local template = script.Parent.Frame.UIFrame.Content.Servers.Frame.Content.ServerTemplate:Clone()
				template.Identifier.Text = i
				template.Nurses.Text = v.Nurses
				template.Patients.Text = v.Patients
				template.Visible = true
				print(i)
			end
		end
	end
end

It simply does not work, doesn’t print or anything. Pretty confused why. As it loops through the whole function that it returns (the table)

Although the serverscript appears to be working as you can view it here,


local MessagingService = game:GetService("MessagingService")
local DatastoreService = game:GetService("DataStoreService")
local ds = DatastoreService:GetDataStore("WardDatastore")

local RandomNameGenerator = require(game.ReplicatedStorage.Modules.RandomNameGenerator)
local ServerName = RandomNameGenerator:RandomName()

local Serverdata = {
	["Wards"] = {
		["TestWard"] = {
			["JobId"] = nil,
			["Nurses"] = nil,
			["Patients"] = nil
		}
	},
}

local server = false

local function deepCopy(original)
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end


game.Players.PlayerAdded:Connect(function(plr)
	if not server == true then
		server = true
		local jobid = game.JobId
		local Nurses = 0
		local Patients = 0
		
		for i, v in pairs(game.Players:GetPlayers()) do
			if v:GetRankInGroup(5562794) >= 50 then
				Nurses = i
			else
				Patients = i
			end
		end
		
		local Clone = deepCopy(Serverdata.Wards)
		Clone.JobId = game.JobId
		Clone.Nurses = Nurses
		Clone.Pateints = Patients
		
		local success,response = pcall(function()
			ds:SetAsync(game.PlaceId, Clone)
		end)
		
		if success then
			print("Succesfully saved data of ward")
		else
			warn(response)
		end
	end
end)

game.ReplicatedStorage.Events.ServerList.OnServerInvoke = function(plr)
	print("Returning serverdata")
	return Serverdata.Wards
end

local Clone = deepyCopy(Serverdata.Wards[servername])

Because you’re looping through the whole Wards table even though the JobId, Nurses and Patients aren’t in there but they’re in the servername table.

So if I’m getting it from the client it wouldn’t be v.Nurses / v.JobId / v.Patients?
image
As seen here it returns nil.

  • Via the server script.
    image

I don’t understand?

And you should type Nurses = Nurses + 1 instead of Nurses = i also do it with the Patients

The function that you gave me returned nil, as it is not finding it through the table (ServerName) which I am trying to copy from the original table (TestWard), so shouldn’t I be copying TestWard instead of ServerName which is returning nil (because it doesn’t exist.)

image

Please show me the code you entered.

nvm I read it.

local Clone = deepyCopy(Serverdata.Wards)
Clone[ServerName][JobId] = JobId
Clone[ServerName][Nurses] = Nurses
Clone[ServerName][Patients] = Patients

Server:

(This is my code currently.)

local MessagingService = game:GetService("MessagingService")
local DatastoreService = game:GetService("DataStoreService")
local ds = DatastoreService:GetDataStore("WardDatastore")

local RandomNameGenerator = require(game.ReplicatedStorage.Modules.RandomNameGenerator)
local ServerName = RandomNameGenerator:RandomName()

local Serverdata = {
	["Wards"] = {
		["TestWard"] = {
			["JobId"] = nil,
			["Nurses"] = nil,
			["Patients"] = nil
		}
	},
}

local server = false

local function deepCopy(original)
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end


game.Players.PlayerAdded:Connect(function(plr)
	if not server == true then
		server = true
		local jobid = game.JobId
		local Nurses = 0
		local Patients = 0
		
		for i, v in pairs(game.Players:GetPlayers()) do
			if v:GetRankInGroup(5562794) >= 50 then
				Nurses = i
			else
				Patients = i
			end
		end
		
		local Clone = deepCopy(Serverdata.Wards.TestWard)
		Clone = ServerName
		Clone.JobId = game.JobId
		Clone.Nurses = Nurses
		Clone.Patients = Patients
		
		local success,response = pcall(function()
			ds:SetAsync(game.PlaceId, Clone)
		end)
		
		if success then
			print("Succesfully saved data of ward")
		else
			warn(response)
		end
	end
end)

game.ReplicatedStorage.Events.ServerList.OnServerInvoke = function(plr)
	print("Returning serverdata")
	return Serverdata.Wards
end

JobId, is the Jobid of the server so you are able to teleport to that server.

Read the update reply I sent I think that’s the correct one

also in the loop do

Nurses = Nurses + 1
and
Patients = Patients + 1
or you’re basically saying Nurses = i which could be any amount

I have updated this via the code (before I sent you the previous code.)
This error is appearing.
image

  • Edit, which appears to be because I am inside studio.

Does it work outside of studio then? or could you send me the line of the error if it’s not working.

Edit: could you send me line 48 in your script?

Edit2: you should remove TestWard from the table once you’re done with it, because it’s just an example and it’s all nil.

Edit3: btw I don’t think it’s necessary to save a serverlist you could use messaging service. Because you’re saving server that could be shutdown if everyone left and it’ll still appear on other servers.

Testing this right now! (30 word limit)


It appears to be erroing ingame, regardless of the jobid being nil/not nil.

And when inside of the client script it keeps returning “TestWard”
image

If I so were to use messaging service, how would I go around it?

You should watch HowToRoblox’s video, because it would take me a while to explain / write a code example for it.