local WorkspaceFolder = workspace.Map
local MapFolder = WorkspaceFolder:GetChildren()
local MapContents = MapFolder:GetChildren()
local Spawns = MapContents.Spawns:GetChildren()
for i,v in pairs(Spawns) do
v.Enabled = true
end
The tree looks like this
i cant say .crossroads bc the map is random every time + each map has same stucture
I get the error ’ attempt to call a nil value’ on the line 3
The error you are experiencing is because you are using Instance | Roblox Creator Documentation incorrectly. :GetChildren returns the children of an instance in the form of an array, not a dictionary. That means that to access specific children you should instead use either Instance.ChildInstanceName or Instance[ChildInstanceName].
local WorkspaceFolder = workspace:WaitForChild("Map")
local MapFolder = WorkspaceFolder:WaitForChild("Crossroads")
local Spawns = MapFolder:WaitForChild("Spawns")
for _, v in pairs(Spawns:GetChildren()) do -- We call :GetChildren in the loop so we aren't using potentially outdated information.
v.Enabled = true
end
However, I understand that you are trying to set up a system that has multiple maps, hence the folders.
In this case you should have several variables defined so you have easy access to the current map. In this case you could do something like this:
local WorkspaceFolder = workspace:WaitForChild("Map")
local CurrentMap = WorkspaceFolder.Crossroads -- Your path to the current map being used
local CurrentSpawns = CurrentMap.Spawns -- Your path the the current map's spawns folder
Know that both CurrentMap and CurrentSpawns can be re-defined later in the script when you are changing to the new map. That way you can then have a function to call that will work on all maps like so.
-- Sets the CurrentSpawns to be enabled or disabled.
local function SetSpawnsEnabled(bool)
for _, spawn in pairs(Spawns:GetChildren()) do
spawn.Enabled = bool
end
end
SetSpawnsEnabled(true) -- Enables all the spawns in the current map
SetSpawnsEnabled(false) -- Disables all the spawns in the current map
The main issue I’m having is getting inside the map folder that is inside of ‘Map’ in workspace.
If you can help me impament this into my round script - i’d be really appreciated
Round Script
local Round = false
local RoundTime = 30
local IntermissionTime = 30
while true do
for i = IntermissionTime, 1, -1 do
game.ReplicatedStorage.Timing.Intermission.Value = true
print(i..' time left of intermission')
game.ReplicatedStorage.Timing.TimeRemaining.Value = i
game.ReplicatedStorage.Timing.InRound.Value = false
wait(1)
end
if Round == false then -- if there is no round rn
Round = true
game.ReplicatedStorage.Timing.Intermission.Value = false
game.Workspace.LobbySpawn.Enabled = false
--[[
load spawns here
--]]
for i,v in pairs(game.Players:GetChildren()) do
v.Character.Humanoid.Health = 0
end
for i = 10, 1, -1 do
game.ReplicatedStorage.Timing.Changing.TextToShow.Value = 'Starting in '..i..' - Get ready!'
wait(1)
end
game.ReplicatedStorage.Timing.TextToShow.Value = 'FIGHT!'
for i,v in pairs(game.Players:GetChildren()) do
v.Character.ForceField:Destroy()
local s = game.ReplicatedStorage["Default Sword"]:Clone()
local s1 = game.ReplicatedStorage["Default Sword"]:Clone()
s.Parent = v.StarterGear
s1.Parent = v.Backpack
wait()
end
for i = RoundTime, 1, -1 do
print(i..' time left of round')
game.ReplicatedStorage.Timing.TimeRemaining.Value = i
game.ReplicatedStorage.Timing.InRound.Value = true
wait(1)
end
local player
local high = 0
for _, plr in pairs(game.Players:GetPlayers()) do
local value = plr.leaderstats['Round Kills'].Value
if value > high then
player = plr
high = value
end
end
print(player.DisplayName..' got most kills')
game.ReplicatedStorage.SendChatMsg:FireAllClients('[👑] '..player.DisplayName..' got the most kills!',Color3.fromRGB(255, 183, 0))
player.leaderstats.Funds.Value = player.leaderstats.Funds.Value + 30
for _,plr in pairs(game.Players:GetPlayers()) do
plr.leaderstats['Round Kills'].Value = 0
end
Round = false
game.Workspace.LobbySpawn.Enabled = true
--[[
disable spawns here
--]]
for i,v in pairs(game.Players:GetChildren()) do
v.Character.Humanoid.Health = 0
for o,r in pairs(v.StarterGear:GetChildren()) do
r:Destroy()
end
end
end
end
Map Load Script (if its needed)
--// vars
local MapFolder = game.ReplicatedStorage.Maps
local MapWorkspace = game.Workspace.Map
local IntVal = game.ReplicatedStorage.Timing.Intermission
--// generator
local mf = MapFolder:GetChildren()
--//
IntVal.Changed:Connect(function()
if IntVal.Value == true then
print('true')
local currentmap = MapWorkspace:GetChildren()
currentmap.Parent = MapFolder
wait(5)
local map = mf[math.random(#mf)]
map.Parent = MapWorkspace
end
end)
Also, you don’t seem to understand functions yet, because you are using another script to do a single action. This can be accomplished by functions as well and doesn’t require communicating between scripts to work.
Now that that’s out of the way, here’s a new version of your round script:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Customization
local RoundTime = 30 -- The time in seconds the round last
local IntermissionTime = 30 -- The time in seconds intermission last
local LoadDifferentMap = true -- Prevents the same map being used twice if set to true
-- Map Generator Objects
local StoredMaps = ReplicatedStorage:WaitForChild("Maps")
local WorkspaceMap = workspace:WaitForChild("Map")
local CurrentMap -- an empty variable for now
-- Other stuff
local LobbySpawn = workspace:WaitForChild("LobbySpawn")
local Timing = ReplicatedStorage:WaitForChild("Timing")
-- Erasure of the variable
ReplicatedStorage = nil -- We no longer need this variable so we are setting it to nil
-- Functions
local function SetSpawnsEnabled(bool) -- our function to enable or disable spawns
for _, spawn in pairs(CurrentMap.Spawns:GetChildren()) do
spawn.Enabled = bool
end
end
local function LoadMap(name) -- the name variable is optional for if you want to load a specific map.
local storedMaps -- an empty variable for now
if CurrentMap then -- This removes the current map if it exist
storedMaps = (LoadDifferentMap and StoredMaps:GetChildren()) or true
SetSpawnsEnabled(false) -- disables the spawns for the map
CurrentMap.Parent = StoredMaps
storedMaps = (not LoadDifferentMap and StoredMaps:GetChildren()) or storedMaps
end
CurrentMap = storedMaps[math.random(#storedMaps) -- Chooses a random map
CurrentMap.Parent = WorkspaceMap
SetSpawnsEnabled(true) -- enables the spawns for the map
end
-- The main loop
while true do
-- Intermission
Timing.Intermission.Value = true -- we can put this before the intermission loop.
Timing.InRound.Value = false -- we can put this before the intermission loop.
LobbySpawn.Enabled = true
local t = 0
repeat -- we use a repeat loop instead of a for loop
Timing.TimeRemaining.Value = i
print(i..' time left of intermission')
t += wait(1) -- wait will return the amount of time waited. This enables us to account for longer times in the event of lag. Also, += is a compound operator. It is the same as t = t + wait(1)
until t >= IntermissionTime -- Will repeat the loop until we have waited the necessary IntermissionTime
Timing.Intermission.Value = false
Timing.InRound.Value = true -- For future reference, look up what the negation operator 'not' does. It can help in cases like this so we don't have to use two separate values.
-- Load the map
LoadMap() -- Calls our function. If we wanted to load a specific map we would do: LoadMap(ExampleMapName)
LobbySpawn.Enabled = true -- disable the lobby spawn
-- all your other game code and stuff
wait(RoundTime)
end
local function LoadMap(name)
local storedMaps
if CurrentMap then
storedMaps = (LoadDifferentMap and StoredMaps:GetChildren()) or true
SetSpawnsEnabled(false)
CurrentMap.Parent = StoredMaps
storedMaps = (not LoadDifferentMap and StoredMaps:GetChildren()) or storedMaps
end
CurrentMap = storedMaps[math.random(#storedMaps)] -- error here
CurrentMap.Parent = WorkspaceMap
SetSpawnsEnabled(true)
end
i dont really know how to fix it myself, i tried adding wait just in case it hadn’t loaded yet but that wasn’t the case
Hmm. This means that the storedMaps variable I created has not been defined yet and is still nil. Therefore, because it isn’t a table the # operator can’t be used on it, thus producing the error. I wrote this code without testing it so it isn’t immune to the infant fatality curve.
I realize my error. I forgot to define the variable outside of the if CurrentMap then statement, so because you don’t yet have a map it won’t define it. To fix replace or edit the orginal function using this:
local function LoadMap(name)
local storedMaps = (LoadDifferentMap and StoredMaps:GetChildren()) or false -- I should have originally defined the variable here. My bad.
if CurrentMap then
SetSpawnsEnabled(false)
CurrentMap.Parent = StoredMaps
storedMaps = (not LoadDifferentMap and StoredMaps:GetChildren()) or storedMaps
end
CurrentMap = storedMaps[math.random(#storedMaps)] -- error no longer here :)
CurrentMap.Parent = WorkspaceMap
SetSpawnsEnabled(true)
end