Script working fine until I add this line?

I was making a script that edits the default touchscreen GUI and it works fine:
LocalScript in game.StarterGui

local Properties = {
	Image = "rbxasset://textures/ui/TouchControlsSheet.png";
	MouseDown = {
		Size = Vector2.new(174, 174);
		Pos = Vector2.new(0, 222);
	};
	MouseUp = {
		Size = Vector2.new(174, 174);
		Pos = Vector2.new(176, 222);
	};
}
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
script.Parent.ChildAdded:Connect(function(Object)
	if Object.Name == "TouchGui" then
		local TouchControlFrame = Object:WaitForChild("TouchControlFrame")
		local JumpButton = Instance.new("ImageButton")
		JumpButton.BackgroundTransparency = 1
		JumpButton.Size = TouchControlFrame.JumpButton.Size
		JumpButton.Position = TouchControlFrame.JumpButton.Position
		JumpButton.Image = Properties.Image
		JumpButton.ImageRectSize = Properties.MouseUp.Size
		JumpButton.ImageRectOffset = Properties.MouseUp.Pos
		JumpButton.AutoButtonColor = false
		TouchControlFrame.JumpButton:Destroy()
		JumpButton.MouseButton1Down:Connect(function()
			JumpButton.ImageRectSize = Properties.MouseDown.Size
			JumpButton.ImageRectOffset = Properties.MouseDown.Pos
		end)
		JumpButton.MouseButton1Up:Connect(function()
			JumpButton.ImageRectSize = Properties.MouseUp.Size
			JumpButton.ImageRectOffset = Properties.MouseUp.Pos
		end)
		JumpButton.MouseLeave:Connect(function()
			JumpButton.ImageRectSize = Properties.MouseUp.Size
			JumpButton.ImageRectOffset = Properties.MouseUp.Pos
		end)
		JumpButton.Parent = TouchControlFrame
	end
end)

until I add this line after line 13?

local Humanoid = Char:WaitForChild("Humanoid")

I made sure it exists by printing it and it does exist so I have no clue, please help.

Mind telling us what the error is?

Well the problem is there is no error in the console, idk why that is.

1 Like

Well… it would be helpful if you told us what the problem is then.

Edit: Oh i get your problem now, sorry…

That’s strange… Did the output say anything about infinite yield possible?

What happens if you try printing Char.Humanoid.Name on that line?

I think I know what your problem is! After the line that breaks the script, you connect a function to script.Parent.ChildAdded, the problem is, that when you add the WaitForChild to wait for the humanoid, the ChildAdded runs before it gets to the line that connects it to the function.
Try waiting for the humanoid after the ChildAdded function.

2 Likes

Thank you so much, script works now!

2 Likes