if game:IsLoaded() == false then
task.wait()
end
local CountryModels = script.Parent.Models
local Teams = game.ReplicatedStorage.Teams
for i,v : Model in CountryModels:GetChildren() do
for i, r in v:GetChildren() do
local Country = Teams[v.Name]
local Population = Country.Population
local ResidentValue = Instance.new("NumberValue")
ResidentValue.Name = "Resident"
ResidentValue.Value = math.random(1, Teams[r.Parent.Name].Population.Value / (#r.Parent.Parent:GetChildren()))
ResidentValue.Parent = r
end
end
For each tile, the population is divided, but not evenly, I want all residentvalue to equal to the population in that country.
But all the values added up is way above or below the population.
it looks like it doesnt take into account that there is other number values
Have you tried calculating the sum of all tiles population and then distribute it accordingly?
if game:IsLoaded() == false then
task.wait()
end
local CountryModels = script.Parent.Models
local Teams = game.ReplicatedStorage.Teams
for i, v in ipairs(CountryModels:GetChildren()) do
local Country = Teams[v.Name]
local Population = Country.Population.Value
local TotalPopulation = 0
local TileCount = 0
for i, r in ipairs(v:GetChildren()) do
TileCount = TileCount + 1
end
for i, r in ipairs(v:GetChildren()) do
local ResidentValue = Instance.new("NumberValue")
ResidentValue.Name = "Resident"
if i < TileCount then
local tilePopulation = math.floor(Population / TileCount)
ResidentValue.Value = math.random(tilePopulation - 5, tilePopulation + 5) -- Add some randomness to each tile's population
TotalPopulation = TotalPopulation + ResidentValue.Value
else
ResidentValue.Value = Population - TotalPopulation
end
ResidentValue.Parent = r
end
end