Script basically halts other scripts until the player resets

I made a script that opens a door when a ProximityPrompt is triggered - it also enables a script that allows the player to teleport between two defined locations. Everything works smoothly until the player is teleported, in that event the doors cease to function. The door scripts just refuse to run, nothing prints, and no errors appear in the console; the only fix is resetting the player, doing so makes everything work until the player teleports again. Every other script in the game works just fine, it’s only the doors that are broken by it.

local tpEvent = game.ReplicatedStorage.TeleportEvent
local players = game.Players
script.Parent.Touched:Connect(function(hit)
	local part2 = workspace['Tele2']
	local hum = hit.Parent:FindFirstChild("HumanoidRootPart")
	if hum then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		tpEvent:FireClient(player) -- fires a RemoteEvent that fades a Gui in and out
		wait(1)
		hum.Position = part2.Position
		part2.TeleScript.Disabled = true -- disables a script located in the second teleport location, to prevent teleport loops
		script.Disabled = true -- same as above
		wait(2)
		script.Disabled = false
		part2.TeleScript.Disabled = false
	end
end)

The scripts themselves are located in parts, and are disabled until a door is opened. Everything related to the teleportation is done server-side, the only client side events are the fade in elements.

Disabling scripts is a code smell in this case. There are prettier solutions to your teleportation problem.

You can use a RemoteFunction to wait until the GUI is faded out.

1 Like

The issue isn’t really scripts being disabled per-say, after running the teleportation script, everything is enabled except the scripts defined on this line

and this line

And they’re disabled and re-enabled when I need them to.

I believe that @Brickman808 is right on this one. Disabling and reenabling scripts do cause them to react with a delay.

Although Disabled may work in your instance, if the TeleScript is encased in a while true do loop, it’ll keep looping forever even if you set the property to true

I’m just wondering why you couldn’t just use a debounce to prevent the script from firing multiple times

It is a really inefficient and complex approach to this problem. You practically only need one script (maybe some ModuleScripts if you want a fancy solution) to handle your 2 teleporters. Disabling scripts might seem logical, but it only makes the process more complex than it really has to be. That is why I mentioned this as a code smell.

What I recommend is this:

-- Write a TeleporterHandler
local TeleporterHandler = require('TeleporterHandler')
-- Handle teleporters A and B
local teleporterA = TeleporterHandler:new(A)
local teleporterB = TeleporterHandler:new(B)

-- Handle them separately
1 Like

I’ve figured it out, the problem had nothing to do with disabled scripts - I moved the players HumanoidRootPart which apparently breaks everything.

I changed “HumanoidRootPart” to “Torso” and everything is working swimmingly as it should.

I will take what was said in this thread into consideration and use it to clean my scripts up.