所以我是 Roblox 脚本新手,我正在制作一款带有地图加载系统的剑战游戏。但代码返回此错误ServerScriptService.GameLoop:13: attempts to get length of a nil value
这是我的代码:
local config = require(script.Parent.ModuleScript)
local ServerStorage = game:GetService("ServerStorage")
local intermissionCooldown = 20
while true do
if #game.Players:GetPlayers() < 5 then
wait(1)
print("There is not enough players to start the game")
else
print("The game is starting soon!")
wait(intermissionCooldown)
print("Selecting Map")
local selectedMap = config.maps[math.random(1, #config.maps)]
print("Map selected, the round will begin in 5 seconds")
wait(5)
local currentMap = selectedMap:Clone()
currentMap.Name = "Map"
currentMap.Parent = workspace
end
end
老实说,我不知道该尝试什么,而且我在网上找不到解决方案。
问题在于:“#config.maps”,显然 config 没有成员“.maps”
错误告诉您,您正在尝试获取数组的长度,但该对象实际上
nil
不是数组。这指向这一行:#config.maps
。
这表明
config.maps
不存在,但我们无法真正知道这一点,因为我们不知道您在 local config = require(script.Parent.ModuleScript)
中存储了什么。
因此请仔细检查
script.Parent.ModuleScript
并确保返回的表中有一个名为 maps
的字段。