Readability / refactoring teleporter code

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)
1 Like

I think when it comes to readability with new programmers it’s hard. They don’t really understand how scripts work, what order stuff runs in, scopes, or any of that. So when there are a ton of comments they will feel overwhelmed and maybe discard it without trying it at all. I think it looks great and is readable, but to new programmers, maybe not so much. If your goal is readability for new programmers, I would recommend maybe not having as many comments, only important ones for stuff they need to change. Even for more advanced programmers, there’s no need for a comment every
line, since they will already know what 70% of the script does just by looking at it.
Regardless of the comments tho, this is a well written script :+1:

1 Like

Appreciate the feedback, thanks!

Yeah, I did go a little overboard on the comments, in case new programmers needed a walkthrough of every line to ensure that they know what it does, but I think that’s more likely to, like you said, overwhelm and do more harm than good.

Thanks for the advice!

1 Like