How should I stop execution

I have this code:

bodyPosition.Destroying:Once(function()
						humanoid.RootPart.AssemblyLinearVelocity = Vector3.new(0, 50, 0)

						repeat
							task.wait()
						until humanoid.RootPart.AssemblyLinearVelocity.Y < 0

						humanoid.RootPart.AssemblyLinearVelocity -= Vector3.new(0, 500, 0)

						repeat
							task.wait()
						until math.abs(humanoid.RootPart.AssemblyLinearVelocity.Y) < 0.1

						--Do something

					end)

I want to end execution before throwing error due to non-existent HumanoidRootPart
I’ve looked up task.cancel() and coroutine, still very unsure

you can check if an object exists by doing

if HumanoidRootPart then -- if part exists 
	-- do something

else -- part doesn't exists 
	print("part doesn't exists")
	return -- Ends the function
end

Also Im pretty sure you mean HumanoidRootPart and not humanoid.RootPart

Where do I place the check. I inconsistently use both interchangeably (they refer to the same object).
The callback throw error when Humanoid.RootPart is destroyed. I could use multiple if statements, but I don’t like it. I want to end the callback.

Can you explain what your code is supposed to do?

return could work, but I can’t place it in a separate function. It must be in a statement within the function (like if) thus task.defer() won’t work

It just wait until a certain velocity range, the HumanoidRootPart could be destroyed during waiting, throwing an error. I just wonder if could stop it from a seperate thread rather than using if statements. I had another idea: and exist

this could work (my code is slop):


bodyPosition.Destroying:Once(function()
						
						if not (humanoid and humanoid.RootPart) then
							return
						end
						
						humanoid.RootPart.AssemblyLinearVelocity = Vector3.new(0, 50, 0)

						repeat
							task.wait()
						until not humanoid or humanoid.RootPart.AssemblyLinearVelocity.Y < 0

						humanoid.RootPart.AssemblyLinearVelocity -= Vector3.new(0, 500, 0)

						repeat
							task.wait()
						until not humanoid or math.abs(humanoid.RootPart.AssemblyLinearVelocity.Y) < 0.1
						
						if not (humanoid and humanoid.RootPart) then
							return
						end

						local fallDamage = (humanoid.MaxHealth - humanoid.Health) / 4
						applyDamageToHumanoid(humanoid, fallDamage)

						local damagePart = Utility.createInstance("Part", {
							Size = Vector3.new(10, 1, 10),
							CFrame = CFrame.new(humanoid.RootPart.Position - Vector3.new(0, 2, 0)),
							Anchored = true,
							CanCollide = false,
							Material = Enum.Material.Neon,
							Color = Color3.fromRGB(255, 150, 0),
							Transparency = 0.7
						})
						damagePart.Parent = workspace
						local tween = TweenService:Create(damagePart, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Transparency = 1, Size = Vector3.new(15, 0.5, 15)})
						tween:Play()
						Utility.addToDebris(damagePart, 0.5)
					end)

:doh:

What about this? I did a lil rewrite of your code. 50/50 chance it has errors

bodyPosition.Destroying:Once(function()
	humanoid.RootPart.AssemblyLinearVelocity = Vector3.new(0, 50, 0)
	
	while humanoid and humanoid.RootPart and task.wait() do
		local X =  humanoid.RootPart.AssemblyLinearVelocity.Y < 0
		if not X then continue end
		
		local Y = math.abs(humanoid.RootPart.AssemblyLinearVelocity.Y) < 0.1
		if not Y then continue end
		
		break
	end
	
	
	if not humanoid or not humanoid.RootPart then return end
	
	local fallDamage = (humanoid.MaxHealth - humanoid.Health) / 4
	applyDamageToHumanoid(humanoid, fallDamage)

	local damagePart = Utility.createInstance("Part", {
		Size = Vector3.new(10, 1, 10),
		CFrame = CFrame.new(humanoid.RootPart.Position - Vector3.new(0, 2, 0)),
		Anchored = true,
		CanCollide = false,
		Material = Enum.Material.Neon,
		Color = Color3.fromRGB(255, 150, 0),
		Transparency = 0.7
	})
	damagePart.Parent = workspace
	local tween = TweenService:Create(damagePart, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Transparency = 1, Size = Vector3.new(15, 0.5, 15)})
	tween:Play()
	Utility.addToDebris(damagePart, 0.5)
end)

something like this could work.

Uhh then why’d you mark your reply as the solution :skull:
Unless you meant to reply to your own comment?

Thoughts on final?:

local s = true
						while humanoid.RootPart and task.wait() do
							if s then
								if	humanoid.RootPart.AssemblyLinearVelocity.Y < 0 then
									s = false
									humanoid.RootPart.AssemblyLinearVelocity -= Vector3.new(0, 500, 0)
								end
							elseif humanoid.RootPart.AssemblyLinearVelocity.Y < 0 then
								break		
							end
						end

Id probably write this like this:

local s = true
while humanoid.RootPart and task.wait() do
	local RootVel = humanoid.RootPart.AssemblyLinearVelocity
	
	if s then 
		if RootVel.Y < 0 then
			humanoid.RootPart.AssemblyLinearVelocity -= Vector3.new(0, 500, 0)
			s = false
		end
	else	
		if RootVel.Y < 0 then break end
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.