Function returns nil when .Touched is used

For some reason, the function for a character pinning down another player with it’s claws is returning nil whenever .Touched detects the part of the target.

I put a print before the return, yet when it returns nil, it prints nil before the print statement actually prints what I meant to return:

-- Ability Function:
function mainFunctions.Ability(Player, Type)
	if not AbilityCool[Player] and not Ability[Player].State then
		local Character = Player.Character
		local CreatureStats = baseStats[Character.Folder.Creature.Value]
		if Character.Folder.Stage.Value == "Adult" then
			if CreatureStats[Type] then
				Ability[Player].State = true
				if CreatureStats[Type] == "Pin" then
                        local Hit = false
						local RoarID = Sounds.Extras["Charge Roars"][Character.Folder.Creature.Value]
						local Sound = Instance.new("Sound", Character.Head)
						local Touch
						local Info = {CoolDown = 20}
						Character.Humanoid.AutoRotate = false
						Character.Humanoid:Move(Character.PrimaryPart.CFrame.LookVector)
						Character.Folder.Stamina.Value -= (Character.Folder.MaxStamina.Value * .3)
						Sound.SoundId = RoarID
						Sound.Volume = 1
						Sound:Play()
						Touch = Character.AttackPart.Touched:Connect(function(Part)
							if Part.Parent and Part.Parent:FindFirstChild("Humanoid") then
								if not Hit then
									local TargetChar = Part.Parent
									local RoarID = Sounds.Extras.Aoe
									local Sound = Instance.new("Sound", Character.Head)
									local Died = false
									local Align
									Hit = true
									Sound.SoundId = RoarID
									Sound.Volume = 1
									Sound:Play()
									Character.Humanoid:Move(Vector3.new(0,0,0))
									if (Character.Humanoid.MaxHealth - TargetChar.Humanoid.MaxHealth) > 400 then
										spawn(function()
											TargetChar.Humanoid.Died:Connect(function()
												Died = true
												Align:Destroy()
												TheriPin[Player].Target = nil
												TheriPin[Player] = {}
												Character.Humanoid.AutoRotate = true
												Character.PrimaryPart.Anchored = false
												Character.Humanoid.WalkSpeed = Character.Folder.Speed.Value
												task.spawn(function()
													AbilityCool[Player] = true
													wait(10)
													AbilityCool[Player] = false
												end)
												Ability[Player].State = false
												print(Info)
												return Info
											end)
										end)
										Align = Instance.new("AlignPosition", Character.RightHand)
										Align.Attachment1 = TargetChar.PrimaryPart.Attachment
										Align.Attachment0 = Character.RightHand.PinAttachment
										Align.RigidityEnabled = true
										Align.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
										Align.Responsiveness = 10000
										TheriPin[Player] = {}
										TheriPin[Player].Target = TargetChar
										Character.Humanoid:Move(Vector3.new(0,0,0))
										Character.Humanoid.WalkSpeed = 0
										Character.PrimaryPart.Anchored = true
										TargetChar.Humanoid.WalkSpeed = 0
										ReplicatedStorage.Events.Pinned:FireClient(game.Players:GetPlayerFromCharacter(TargetChar))
										wait(8)
										if not Died then
											spawn(function()
												AbilityCool[Player] = true
												wait(10)
												AbilityCool[Player] = false
											end)
											ReplicatedStorage.Events.Pinned:FireClient(game.Players:GetPlayerFromCharacter(TargetChar))
											Align:Destroy()
											TheriPin[Player].Target = nil
											TheriPin[Player] = {}
											Character.Humanoid.AutoRotate = true
											Character.PrimaryPart.Anchored = false
											TargetChar.Humanoid.WalkSpeed = TargetChar.Folder.Speed.Value
											Character.Humanoid.WalkSpeed = Character.Folder.Speed.Value
											Ability[Player].State = false
											print(Info)
											return Info
										end
									else
										spawn(function()
											AbilityCool[Player] = true
											wait(10)
											AbilityCool[Player] = false
										end)
										TargetChar.Humanoid:TakeDamage(300)
										Character.Humanoid:Move(Vector3.new(0,0,0))
										Character.Humanoid.AutoRotate = true
										Ability[Player].State = false
										print(Info)
										return Info
									end
								end
							end
						end)
						wait(1)
						if not Hit then
							Character.Humanoid.AutoRotate = true
							Touch:Disconnect()
							spawn(function()
								AbilityCool[Player] = true
								wait(10)
								AbilityCool[Player] = false
							end)
							Ability[Player].State = false
							print(Info)
							return Info
						end
					else
						local Info = {CoolDown = .1}
						Ability[Player].State = false
						return Info
					end
				end
			else
				local Info = {CoolDown = .1}
				Ability[Player].State = false
				return Info
			end
		end
	end
end

You should use WaitForChild() or FindFirstChild() in some cases
Like this one:

Character:FindFirstChild(Humanoid)

and change this line

local Character = Player.Character

to this

local Character = Player.Character or Player.CharacterAdded:Wait()
1 Like

How does this stop from returning by itself? And it’s not returning the character, it’s returning a table containing info like the cooldown.