ChildAdded works but not ChildRemoved

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    Get my child remove working because its a big part of my game.

  2. What is the issue?
    Since the childremove isn’t working I cant stop my animations…also when I throw my football it flings me because the childremove function isn’t calling so my motor6D can be nil.

  3. What solutions have you tried so far?
    I’ve tried replacing my football with another but the same thing keeps happening. Also when I don’t use childremove it all functions perfectly fine…

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function OnChildRemoved(child)
	if child.Name == "Football" then
		BallGui:Destroy()
		AnimationsModule:StopAllAnimations()
	end
end
function Football.unequip(Ball)
	local motor6d = Ball:FindFirstChild("FootballMotor6D")
	if motor6d then
		motor6d.Part0 = nil
	end
end

function Football.OnChildRemoved(child)
	if child.Name == "Football" then
		Football.unequip(child)
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

can we see the connections you made and also add a print right above the if statement to check if the function is even called at all

local function OnChildRemoved(child)
        print("Called!")
	if child.Name == "Football" then
		BallGui:Destroy()
		AnimationsModule:StopAllAnimations()
	end
end
2 Likes

Your code looks very weird. Do you know how to use object oriented programming or modulescripts? It might be worth checking this thread.

Football.ChildRemoved:Connect(function(instance)
	if instance.Name == "Football" then
		--Code here
	end
end)
1 Like

local function Throw(Origin, Target, Power, Throwtype, Tackled, Player)
	local NewPower = Power
	Player.Character.ChildRemoved:Connect(FootballModule.OnChildRemoved)
	if Football:IsA("MeshPart") then
		if Throwtype ~= 4 then
			if Player.Character.Hitbox:GetAttribute("Position") == "QB" then
				if Player.Character.Hitbox:GetAttribute("Archetype") == "Gunslinger" then
					if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
						NewPower = NewPower + 10
					else
						NewPower = NewPower + 5
					end
				elseif Player.Character.Hitbox:GetAttribute("Archetype") == "Magician" then
					if Player.Character.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
						NewPower = NewPower - 7
					end
				end
			end
			if Throwtype == 1 then
				if Player.Character.Hitbox:GetAttribute("Position") == "QB" then
					if Player.Character.Hitbox:GetAttribute("Archetype") == "Conductor" then
						if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
							NewPower = NewPower * .85
						else
							NewPower = NewPower * .79
						end
					elseif Player.Character.Hitbox:GetAttribute("Archetype") == "Magician" then
						if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
							NewPower = NewPower * .80
						else
							NewPower = NewPower * .77
						end
					end
				else
					if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
						NewPower = NewPower * .75
					else
						NewPower = NewPower * .70
					end
				end
			end
			if Throwtype == 2 then
				if Player.Character.Hitbox:GetAttribute("Position") == "QB" then
					if Player.Character.Hitbox:GetAttribute("Archetype") == "Gunslinger" then
						if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
							NewPower = NewPower * .95
						else
							NewPower = NewPower * .75
						end
					end
				end
			elseif Throwtype == 3 then
				Football.Kick:Play()
				if Kicked then
					NewPower = KickPower
				end
			elseif Throwtype == 5 then
				NewPower = Power * .6
			end
		end
		if Tackled then
			NewPower = NewPower * .6
		end
		Ready = false
		if Throwtype ~= 4 then
			Football.Client.BallGui.PowerFrame.Bar.PowerLabel.Text = tostring(math.min(Power, 100))
		else
			Football.Client.BallGui.PowerFrame.Bar.PowerLabel.Text = tostring(math.min(LastPower, 100))
		end
		Football.CanCollide = true
		if Throwtype == 3 then
			Football:SetAttribute("Kicked", true)
		end
		spawn(function()
			Football.CanTouch = false
			wait(.3)
			Football.CanTouch = true
		end)
		Football.Parent = game.Workspace
		Football:SetAttribute("Thrown", true)
		Player.Character.Hitbox:SetAttribute("Mode", "Running")
		Football:SetAttribute("Catchable", true)
		Football:SetAttribute("Kickable", false)
		Football:SetAttribute("Equipped", false)
		Football:SetAttribute("Throwing", false)
		Football:SetAttribute("Snapping", false)
		Kicked = false
		if Player ~= nil and Player:FindFirstChild("Head") ~= nil then
			if Throwtype == 1 or Throwtype == 2 then
				Origin = (Player.Head.CFrame * CFrame.new(1.5, 0, 0)).p
			elseif Throwtype == 5  then
				Origin = (Player.Head.CFrame * CFrame.new(0, -2, 0)).p
			else
				Origin = Player.Head.CFrame
			end
		end
		local Aim = (Target - Origin).unit
		local SpawnPos = Origin + (Aim * 5)
		local TargetPos = SpawnPos + (Aim * math.max(1, NewPower))
		Football.Position = SpawnPos
		if Tackled == false then
			Football.Velocity = Aim * NewPower
		else
			Football.Velocity = (Aim * NewPower) + Vector3.new(0, math.random(5,10), 0)
		end
		Football.CFrame = CFrame.new(SpawnPos, TargetPos) * CFrame.Angles(math.pi/2, 2, 0)
		local BodyVelocity = Instance.new("BodyVelocity", Football)
		BodyVelocity.MaxForce = Vector3.new(1e3, 1e3, 1e3)
		BodyVelocity.P = NewPower * 100
		if Tackled == false then
			BodyVelocity.Velocity = (TargetPos - Football.CFrame.p).unit * (NewPower - 5)
		else
			BodyVelocity.Velocity = ((TargetPos - Football.CFrame.p).unit * NewPower) + Vector3.new(0, math.random(0,15), 0)
		end
		-- GRAVITY --
		Football.FlyingSound:Stop()
		Football.FlyingSound:Play()
		FreeFalling = true
		Ready = true
		local i = 0
		local Gravity0 = Football:GetAttribute("Gravity")
		local Gravity1
		local AirRessitance = Football:GetAttribute("AirRessitance")
		while FreeFalling do
			Gravity1 = Gravity0 + (AirRessitance * (1 - math.abs(Football.CFrame.UpVector.Y)))
			if Throwtype ~= 1 or Throwtype ~= 2 or Throwtype ~= 3 or Throwtype ~= 4 or Throwtype ~= 5 then
				BodyVelocity.Velocity = BodyVelocity.Velocity + Vector3.new(game.Workspace.GlobalWind.X * .000115, Gravity1 * RunService.Heartbeat:Wait(), 0)
			else
				BodyVelocity.Velocity = BodyVelocity.Velocity + Vector3.new(game.Workspace.GlobalWind.X * .000115, -38 * RunService.Heartbeat:Wait(), 0)
			end
			local P = Football.CFrame.p
			if Tackled == false then
				if Throwtype == 1 or Throwtype == 5 then
					i = i + 0.18
					if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
						Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(math.pi/2, math.pi * i, math.pi/2)
					else
						Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(.75 * math.pi/2, math.pi * i, .75 * math.pi/2)
					end
				elseif Throwtype == 2 then
					i = i + 0.16
					if Player.Character.Hitbox:GetAttribute("Position") == "QB" then
						if Player.Character.Hitbox:GetAttribute("Archetype") == "Gunslinger" then
							if Player.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
								Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(math.pi/2, math.pi * i, math.pi/2)
							else
								Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(.75 * math.pi/2, math.pi * i, .75 * math.pi/2)
							end
						end
					end
				elseif Throwtype == 3 then
					i = i + 0.17
					Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(math.pi * i, .75 * math.pi/2, .75 * math.pi/2)
				elseif Throwtype == 4 then
					i = i + 0.08
					Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(.75 * math.pi/2, math.pi * i, .75 * math.pi/2)
				end
			else
				i = i + 0.1
				if Throwtype == 1 or Throwtype == 2 or Throwtype == 5 then
					Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(.75 * math.pi/2, math.pi * i, .75 * math.pi/2)
				else
					Football.CFrame = CFrame.new(P, P + BodyVelocity.Velocity + Buffer) * CFrame.Angles(math.pi/2, math.pi * i, math.pi/2)
				end
			end
		end
	end
end
1 Like

Alright so the module script version is working! However the other one isn’t.

1 Like

Im having the same issues with ChildRemoved not working.

1 Like

I think Roblox is having a ChildRemove error. Which is terrible. Mine still isn’t working either.

1 Like

At least from the code you’ve provided, I don’t see how the ChildRemoved Event would fire.

It seems like you’re expecting something to be removed from the Player’s character, but I don’t see anything being reparented or destroyed in your code.

Maybe this is part of the issue? Or maybe there’s code for this that we’re not able to see

1 Like

In the function there is an unequip function within the ChildRemove which allows the script to understand when the “ball” is removed from the child that’s what is firing.

I’m not sure what you mean by this:

How does it ‘understand’? I think the problem might be that you aren’t familiar with how events work.

Here you are telling the game that when something is removed from the character, you want to run the function ‘FootballModule.OnChildRemoved’. But something has to actually be removed from the character in order for that code to run, it won’t just run automatically.

That’s why @Sindious suggested you print out something so you can see if the event is actually being triggered. Try his suggestion and see if anything prints out

I have this same script in my other game and it works Perfectly…now in this other game it isn’t working and the codes are identical. I know how events work. I’ve been scripting on Roblox since 2012. Something is happening with the ChildRemoved in Roblox.

1 Like