I’m making a disaster game and I’m trying to load disasters.
Code
local Disasters = {"Zombie Attack","Tsunami","SANIC"}
while true do
wait(25)
local DisasterTable = Disasters[math.random(1,#Disasters)]
print("Disaster: "..DisasterTable)
local ServerStorage = game:GetService("ServerStorage")
local Disaster = ServerStorage.DisasterTable:Clone()
Disaster.Name = "LoadedDisaster"
Disaster.Parent = workspace
wait(25)
workspace.LoadedDisaster:Destroy()
end
However, it says that DisasterTable is not a valid member of ServerStorage. I’m not looking for an object named DisasterTable however.
You are. You directly indexed DisasterTable on ServerStorage.
DisasterTable isn’t a table either its just a random selection from the table at the top so its more like “ChosenDisaster”
With that, just do this ServerStorage[DisasterTable] or change the DisasterTable to whatever new variable name if you decide changing it.
Using . is syntax sugar for the [ ] if you need ask why instead of . since it requires a specific index based on name instantly defined opposed to [ ] that accepts variables and etc.
I recommend keeping ServerStorage variable outside the loop so you don’t have to define it constantly when the loop runs an iteration.
1 Like