Hi,
I’ve created a teleporter that allows a player to either:
- Teleport to a different experience (place);
- Teleport to a position in the workspace
I’ve had a look, and documented my code, which makes sense to me; I’m wondering whether I may have tunnel vision about how modular and easily integrated it would be if used in other experiences.
At the end of the day, my main use case for this is not for it to gain popularity in the assets library, but I’m conscious of whether it would be easily understood by someone new to programming, and whether it follows standard ROBLOX Luau practices in general.
Any feedback is appreciated!
-- Unfortunately, a global variable for debounce
local debounce = false
-- Function to handle teleporting between places (experiences)
local function TeleportPlace(partTouched, ...)
-- Variable declared for later use
local PLACE
-- Iterates through the parameters passed to the function
for k, v in pairs({...}) do
-- Equality check of data type; if number then succeeds
if type(v) == "number" then
-- Assigns value of currently iterated variable to variable declared earlier
PLACE = v
end
end
-- Equality check of existence of humanoid; checks whether a player has touched the part
if not partTouched.Parent:FindFirstChild("Humanoid") then return end
-- Assigns the parent of the touched part to a variable
local char = partTouched.Parent
-- Finds the player within the "Players" folder
local plyr = game:GetService("Players"):FindFirstChild(char.Name)
-- Teleports the player to the value of PLACE
game:GetService("TeleportService"):TeleportAsync(PLACE, {plyr})
end
-- Function to handle teleporting between positions in the workspace
local function TeleportPosition(partTouched, ...)
-- Variable declared for later use
local POSITION
-- Iterates through the parameters passed to the function
for k, v in pairs({...}) do
-- Equality check of data type; if vector then succeeds
if type(v) == "vector" then
-- Assigns value of currently iterated variable to variable declared earlier
POSITION = v
end
end
-- Assigns the parent of the touched part to a variable
local char = partTouched.Parent
-- Moves the player's character's model to the value of the variable declared earlier
char:MoveTo(POSITION)
end
-- Function to handle fetching values and modes
local function Main(partTouched)
-- Checks whether the part has been touched in the last 2 seconds
if debounce then return end
-- Prevents the function from being called again within the next 2 seconds
debounce = true
-- Fetches configuration values
local CONFIG = script.Parent.Configuration
local MODE = CONFIG.Mode.Value; local PLACE = CONFIG.Place.Value; local POSITION = CONFIG.Position.Value; local SLEEP = CONFIG.Sleep.Value
-- Assigns previously declared functions to a dictionary for ease of adding more functions later
-- Also prevents the need for nested if-else statements
local modes = {
[0] = TeleportPlace,
[1] = TeleportPosition
}
-- Calls the relevant function attached to the mode configuration variable, passing parameters to variadic functions
modes[MODE](partTouched, PLACE, POSITION)
-- Configurable wait time
wait(SLEEP)
-- Allows the function to be called again
debounce = false
end
-- Main function calls when the part is touched
script.Parent.Field.Touched:Connect(Main)