table.Insert() not saving?

I have a script that checks a list of disasters to see which one is selectable

Then uses table.insert() to insert the disaster name in a table if it is

Script:

local AvailableMaps = {}	for i, disaster in pairs(Disasters) do	-- Check enabled	if disaster.Enabled == false then return end	-- Check map specific	if disaster.isMapSpecific == true then	if workspace.Map.Map_Name.Value ~= disaster.SpecificMap then return end	end	-- Check if only unique map disasters	if UniqueMapDisastersOnly == true then	if workspace.Map.Map_Name.Value ~= disaster.SpecificMap then return end	end	-- Add disaster to table	table.insert(AvailableMaps, disaster.Name)	for i, v in pairs(AvailableMaps) do	print(v) -- This prints correctly the disasters	end	end	for i, v in pairs(AvailableMaps) do	print(v) -- this doesn't print anything	end 

Although the table “AvailableMaps” is outside the for loop, printing the things inside the table in the for loops does return the desired disasters, but doing it outside doesn’t print anything, it returns nil

Why does this happen? How could I fix it? HELP

4 Likes

Is there something removing data outside the for loop? If not then maybe try this work around:
1.Create a folder inside the script
2.Create a stringvalue inside the folder with a value of the disaster’s name everytime you add
a disaster’s name

(Optional)
3.Delete all the string value inside the folder if no longer needed.

2 Likes

There’s nothing removing data from the table, and yes your solution sounds good but I’m wondering why doesn’t it work?

1 Like

There is no clear reason why your script is not working, could you show the Disaster table?

Just to give a simple example this reduced code does the same as yours and yes it SHOULD work:

local AvailableMaps = {} local Disasters = {	["Disaster1"] = {Enabled = true, Name = "doom"},	["Disaster2"] = {Enabled = true, Name = "fire from sky"},	["Disaster3"] = {Enabled = true, Name = "tacos from sky"}, } for i, disaster in pairs(Disasters) do	-- Check enabled	if disaster.Enabled == false then return end	-- Add disaster to table	warn("going to add:", disaster.Name)	table.insert(AvailableMaps, disaster.Name) end -- Print the table for i, v in pairs(AvailableMaps) do	print(v) end 

To me, I dont like to use intValues, stringValues everywhere in folders and stuff. Don’t give up with your table based approach, tables are pretty handy

I’ll explain further, every second ingame the Disaster Event is fired, then in another script, there’s a connection to the event and in that script there is a 20% chance of ocurring (right now its 100% for testing purposes), now here comes the problem part, there’s a custom function to get a random disaster which is the one you see, I’ll show it here:

local function GetRandomDisaster()	local SelectedDisaster = nil	local SelectedDisasterName = nil	AvailableMaps = {}	for i, disaster in pairs(Disasters) do	-- Check enabled	if disaster.Enabled == false then return end	-- Check map specific	if disaster.isMapSpecific == true then	if workspace.Map.Map_Name.Value ~= disaster.SpecificMap then return end	end	-- Check if only unique map disasters	if UniqueMapDisastersOnly == true then	if workspace.Map.Map_Name.Value ~= disaster.SpecificMap then return end	end	-- Add disaster to table	table.insert(AvailableMaps, disaster.Name)	for i, v in pairs(AvailableMaps) do	print(v) -- THIS PRINTS "TORNADO"	end	task.wait(2)	end	task.wait(3)	for i, v in pairs(AvailableMaps) do	print(v) -- THIS DOESN'T PRINT ANYTHING	end	SelectedDisaster = AvailableMaps[math.random(1, #AvailableMaps)]	SelectedDisasterName = SelectedDisaster.Name	return SelectedDisaster, SelectedDisasterName end 

And this is my disaster table:

local Disasters = {	["Tsunami"] = {	Name = "Tsunami",	isMapSpecific = false,	SpecificMap = "None",	Enabled = false,	Disaster_Function = function() --[[DisasterModule:TsunamiDisaster()]] end},	["Tornado"] = {	Name = "Tornado",	isMapSpecific = false,	SpecificMap = "None",	Enabled = true,	Disaster_Function = function() --[[DisasterModule:TornadoDisaster()]] end} } 

I’m gonna cry

Well, I don’t know why it wasn’t working but I fixed it by not using "then return end"

Code:

local function GetRandomDisaster()	local SelectedDisaster = nil	local SelectedDisasterName = nil	AvailableMaps = {}	for i, disaster in pairs(Disasters) do	local map_Enabled = false	local map_MatchSpecific = true	local map_MatchUnique = true	-- Check enabled	if disaster.Enabled == true then	map_Enabled = true	end	-- Check map specific	if disaster.isMapSpecific == true then	if workspace.Map.Map_Name.Value ~= disaster.SpecificMap then	map_MatchSpecific = false	end	end	-- Check if only unique map disasters	if UniqueMapDisastersOnly == true then	if workspace.Map.Map_Name.Value ~= disaster.SpecificMap then	map_MatchUnique = false	end	end	-- Add disaster to table	if map_Enabled and map_MatchSpecific and map_MatchUnique then	table.insert(AvailableMaps, disaster.Name)	end	end	task.wait(3)	for i, v in pairs(AvailableMaps) do	print(v) -- NOW THIS PRINTS ALL THE DISASTERS	end	SelectedDisaster = AvailableMaps[math.random(1, #AvailableMaps)]	SelectedDisasterName = SelectedDisaster.Name	return SelectedDisaster, SelectedDisasterName end 

using return early in a function just stops the entire thing, so that might be the reason why the value came out as nothin

but, good job in finding the solution!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.