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)
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
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
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.
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.)
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
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.