Hello!
I’ve been developing a game that includes a user-generated map feature. Players can create maps, write event scripts, and load these maps into the game using InsertService. Recently, Roblox introduced a new Ban API, which simplifies the process of banning users. This is a great feature, but map creators (users who design maps for my game) could potentially exploit this function.
Thus, I’ve been working on implementing restrictions for scripts to prevent abuse, but I’ve encountered an issue.
Problem:
When we enable scripts in-game, there’s no reliable way to debug them or execute just the first line of code for testing purposes. I attempted to fix this issue using a particular principle:
This image is from the TRIA.os server.
This solution works for a single script and restricts the use of certain modules. However, I wanted to design a more advanced Moduled Event Script System. As we know, in C++ we can use classes, so I tried replicating this behavior in Luau using metatables.
Everything worked fine, but when we require(module)
, a non-initialized module gets initialized and starts executing its code immediately. For example:
Server Script:
-- Type Map is a custom type
local CurrentMap: Map = randomMap()
local mapHandler = require(CurrentMap.MapHandler) -- We're requiring MapHandler or ModuleEventScript
CurrentMap.MapHandler:
local Players: Players
-- Some code
Players = game:GetService("Players")
-- Some code
Players:BanAsync(--[[ban someone]])
-- This code will execute with server-side permissions to ban players
local module = {}
-- Functions
return module
The issue arises because this code executes as soon as the module is required. I’m aware of the setfenv function, but I need a way to intercept the module’s environment before it executes.
Question:
How can I set a module’s environment before requiring it, to prevent its code from executing immediately?
Thanks!