Hello! I was attempting to make a teleport script that plays an “animation” by tweening 4 GUIs to each go towards the center on 4 different sides of the screen and then return to their original position, however I ran into an error after using the teleport multiple times stating “script:5: attempt to index nil with ‘PlayerGui’”. This error appears to be random (as far as I can tell), and completely breaks the script when it appears. I have searched multiple topics relating to tweening on the forum, but could not find anything that fixed my problem. How should I go about fixing this issue?
Here’s the script.
Teleporter = script.Parent
function onTouched(hit)
script.Disabled = true
print("Teleporter Hit")
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Top:TweenPosition (UDim2.new(0, 0, -0.5, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Bottom:TweenPosition (UDim2.new(0, 0, 0.5, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Right:TweenPosition (UDim2.new(0.5, 0, 0, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Left:TweenPosition (UDim2.new(-0.5, 0, 0, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.WhiteTop.Frame:TweenPosition (UDim2.new(0, 0, 0, 0, "Out", "Quint", 3))
wait(2)
game.Players:GetPlayerFromCharacter(hit.Parent).Character.Torso.CFrame = CFrame.new(15, 5.5, -827.5) * CFrame.fromEulerAnglesXYZ(0, math.rad(0), 0)
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Top:TweenPosition (UDim2.new(0, 0, -1.1, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Bottom:TweenPosition (UDim2.new(0, 0, 1.1, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Right:TweenPosition (UDim2.new(1.1, 0, 0, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.CartoonTween.Left:TweenPosition (UDim2.new(-1.1, 0, 0, 0, "Out", "Bounce", 5))
game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.WhiteTop.Frame:TweenPosition (UDim2.new(0, 0, -0.1, 0, "Out", "Quint", 3))
wait(0.1)
script.Disabled = false
end
connection = Teleporter.Touched:connect(onTouched)