这是我的模块脚本:
local phases = {}
local function getRoleAssignments(playersToAssignRoles, roles)
--Assigns a random player to each role then removes player from list so player can't get two roles.
--Not every role will be assigned if not enough players
for role, _ in pairs(roles) do
local player = nil
if 0 < #playersToAssignRoles then
player = playersToAssignRoles[math.random(1,#playersToAssignRoles)]
end
roles[role] = player
table.remove(playersToAssignRoles,table.find(playersToAssignRoles,player))
end
return roles
end
function phases.loadGame(players, roles)
local map = getRandomMap()
print(players) --Prints a full table (just one player because just me testing)
local roleAssignments = getRoleAssignments(players, roles)
print(players) --Prints an empty table
end
这就是我在主脚本中实现它的方式。
local playersPlaying = Players:GetPlayers()
roles = Phases.loadGame(playersPlaying, roles)
尽管我根本没有引用它,但players
调用后该表变成了空。getRoleAssignments(players, roles)
我仅将其传递到函数中,该函数应该创建文档中所述的本地副本。但是,players
在我运行时调用 print 语句显示的函数后,该表是空的。
我希望该players
表保留其数据。我试图确保标识符不相同,但没有成功。我厌倦了将players
表存储在单独的局部变量中并将其传递到函数中,但它不起作用。不过,不调用该函数确实会导致players
表保留其数据。