What am I doing? I am trying to make a 4x strategy game that holds all of the game stats on a module, because I think it would be less taxing on performance. I have 100 or so city models scattered throughout the map and it would be annoying to put all of them in a table individually, so I was hoping to be able to let the computer get that by using loops. I am trying to take all of the cities on the map and create a dictionary that holds information specific to that city, and I want to call on that information when the city is clicked on. The problem is, is that the cities register when I use the method to create the tables on the server, but when I try to get that same information from the local script it acts like I never created anything and returns nil. What could I do??
local script: --A local script inside of starterpack, when player mouses over cities it will give you the information that is written inside of the modules.
local StatsModule = require(game.ReplicatedStorage.Modules.Stats)
--When i try to get the cities out of the local script it returns nil
--and tells me that cityName doesn't exist even after I created the cityName
--using BuildCityTables()
for i, v in pairs (StatsModule.Cities) do
print(v.CityName)
end
Module script --located in game.ReplicatedStorage in a folder called Modules.
local Stats = {}
Stats.Cities = {}
return Stats
Server Script --located inside of the serverscriptstorage
local StatsModule = require(game.ReplicatedStorage.Modules.Stats)
function BuildCityTables()
for i, cityModel in pairs(game.Workspace.CityModels:GetChildren()) do
StatsModule.Cities[cityModel.CityName.Value] = {
CityName = cityModel.CityName.Value,
CityDescription = "blah blah blah",
CityCountry = "none",
CityPopulation = 300000
}
end
end
BuildCityTables()
--now if i was to count all of the cities in the server script, it would
--print all of the city names I got from the stringvalue object inside of the model.
for i, v in pairs (StatsModule.Cities) do
print(v.CityName)
end
The problem here is that the Stats table from the ModuleScript is returned and then you’re editing the table returned, however this does not affect the ModuleScript itself.
Might be best to build a Folder to hold ValueBases that represent the City data.
Well, I don’t think I understand why you would use loops when the cities would supposedly all have their unique stats, which you just would have to write out.
There are 100+ cities right?
Or are you trying to convert your stats using value objects into module script form?
Trying to convert the stats into module script form. But all of the city info will be dynamic, like population will grow and fall, city owners will change depending on who takes it. Stuff like that.
The object value is just the city description and name, when the game starts it will build a list of tables based off all the object values containing city names placed in workspace. the rest of the values will be dynamic, such as population, city owner, income.
When player clicks on a city, you could make the client-code send a “query”-request to the server for that particular city. Then have the server process this “query”-request, find the city in its server-side lookup-table/array, extract the information and send back a “result”-response to the client.
You should understand, that Lua-script-code’s variables/tables/arrays are not “magically” replicated between server and clients. Only objects within the Workspace / ReplicatedFirst / ReplicatedStorage gets replicated from server to clients. - To get Lua-script-code’s variables/tables/arrays “replicated”, you need to code that yourself, using RemoteEvent or similar. - Or as ReturnedTrue suggest; make a Folder-object in Workspace or ReplicatedStorage, where you then create ...Value-objects that contain the information that should be available on all clients.