Hi! I am having some problems with my teleporters in my game. I often get reports from players that the teleporters sometimes break, leaving most players in a server stuck in a single room. Here is the code, with some comments to help anyone understand why things are the way they are.
DestinationPos = script.Parent.Parent.Parent.tp2.tpto1.Position --The coordinates the player is teleported to
sound = script.Parent.Parent.Parent.tp2.tp2.pop --A sound effect that plays at the teleporter's destination target
local Remote = game.ReplicatedStorage.ambient --To change the lighting to whatever I need it to be
local Remote2 = game.ReplicatedStorage.fade --A fadeout transition
local Players = game:GetService("Players")
db = false
function teleport() --The function that is triggered when the player touches the teleporter
local player = Players:getPlayerFromCharacter(hit2.Parent)
Remote2:FireClient(player)
wait(.3)
hit2.Parent.Torso.CFrame = CFrame.new(DestinationPos)
Remote:FireClient(player,"forest") --Changes lighting to the "forest" lighting
if player.PlayerGui:FindFirstChild("emotegui") then --The game contains morphs. This code checks what character the player is, and if it is a certain character, then the sound effect won't play.
local charactercheck = player.PlayerGui:FindFirstChild("emotegui").Frame.character.Value
if charactercheck ~= "something" then
sound:Play()
end
end
end
function onTouched(hit)
if not db then
if hit.Name == "Torso" then
hit2 = hit
elseif hit.Name == "Left Leg" or hit.Name == "Right Leg" or hit.Name == "Left Arm" or hit.Name == "Right Arm" or hit.Name == "Head" or hit.Name == "HumanoidRootPart" then
hit2 = hit.Parent.Torso
else
if hit.Parent.Parent.Torso then
hit2 = hit.Parent.Parent.Torso --I do these checks in case any part not located inside of a player comes in contact with the teleporter which would cause it to break. This still doesn't fix my issue.
end
end
if hit2.Name == "Torso" then --Call the teleport function if the hit2 ends up as "Torso"
db = true
teleport()
wait(.1)
db = false
end
end
end
script.Parent.Touched:connect(onTouched)
If anyone with more coding experience than me could look this over and find an issue with it, that would be much appreciated! This is a really annoying bug for me because it always happens while I am not in the server, and I cannot find an error message in the F9 menu. If you need more information than this, just ask. Thank you!