onTouched Tweening error

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)

1 Like

hit.Parent isn’t always going to reference a player, so you should make sure that it is does before going on.

function onTouched(hit)
     local player = game.Players:GetPlayerFromCharacter(hit.Parent)
     if player then
          -- do stuff
     end
end

Also, your code is incredibly repetitive. Are you sure there isn’t an easier way to do this? Such as put the elements you want to tween inside of a frame, and tween just the frame instead of all of the elements?

2 Likes

Thanks your your help!
I’m aware that my code is super messy and repetative, I’m still not the best coder. (I’m just finding out about things like debounce haha)
Also thanks for the suggestion, I will attempt to put everything into a frame!