Is this door teleporting code good? I think it could be better

server side
image

client side

It simply plays a transition and teleports you to the TP object, it disables the proximity prompt so you cant press it through the door and enables the other one. I feel like it could be optimised though or just made better.

–# my inglish sucks please forgive if I mistake anything

In my opinion, if you want more readable code, avoid getting inside if statements.

Server:

for _,v in pairs(workspace.Map.Teleport:GetDescendants()) do
	if v.Name ~= 'TP1' or  v.Name ~= 'TP2' then continue end --Continue if is different
	
	for _,v in pairs(v:GetChildren()) do
		if not v:IsA('ProximityPrompt') then continue end
		
		v.Triggered:Connect(function(plr)
			RS.Event.TransitionPlay:FireClient(plr)
			RS.Event.DoorTP:FireClient(plr, v.Parent)
		end)
	end
end

Your client side is fine for me

Btw about

if v.Name ~= 'TP1' or 'TP2' <- this

Is and error you passing a string and will be always true

I mean the OR Part

Use proper variable names to make your code self documenting (if you come back to your code later you won’t be as confused)

Also reduce nesting.

for _, teleporter in workspace.Map.Teleport:GetDescendants() do
        if teleporter.Name ~= "TP1" or teleporter.Name ~= "TP2" then
                continue
        end

        for _, proximityPrompt in teleporter:GetChildren() do
                if not proximityPrompt:IsA("ProximityPrompt") then
                        continue
                end

                proximityPrompt.Triggered:Connect(function(player: Player) 
                        ReplicatedStorage.Event.TransitionPlay:FireClient(player)
                        ReplicatedStorage.Event.DoorTP:FireClient(player, proximityPrompt.Parent)
                end)
        end
end

1 Like