How do I break a function early?

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

  1. What do you want to achieve? Keep it simple and clear!
    So im making a script that checks if the fireball is hitting the player who casts it, if it does then it would stop the current touched event until the event was fired again.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to stop it in the middle.
script.Parent.Touched:Connect(function(hit)
	i = 0
	humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		if humanoid.Parent.Name ~= _G.ZJUZNSW then
			--How do I end the function here?
		end
	end
	script.Parent.CanCollide = false
	script.Parent.Transparency = 0.5
	script.Parent.Reflectance = 0
	if humanoid then 
		if humanoid.Parent.Name ~= _G.ZJUZNSW then
			print(humanoid.Parent.Name)
			print(_G.ZJUZNSW)
			humanoid.Health = humanoid.Health - 10
			wait(4)
			script.Parent:Destroy()
			end
	end
end)
wait(6)
script.Parent:Destroy()
3 Likes

Use the return keyword to return from a function.

7 Likes

Whenever you want to stop a function, you would use the return keyword. However if you want to break a loop, you would use the break keyword.

I tried something like this

script.Parent.Touched:Connect(function(hit)
	humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		if humanoid.Parent.Name == _G.ZJUZNSW then
			return 
		end
	script.Parent.CanCollide = false
	script.Parent.Transparency = 0.5
	script.Parent.Reflectance = 0
	if humanoid then 
		if humanoid.Parent.Name ~= _G.ZJUZNSW then
			print(humanoid.Parent.Name)
			print(_G.ZJUZNSW)
			humanoid.Health = humanoid.Health - 10
			wait(4)
			script.Parent:Destroy()
			end
	end
wait(6)
script.Parent:Destroy()
	end
end)

but it didnt work?

if statements are based on a condition, to figure out why it’s not returning I recommend adding prints

local Condition = true
if Condition then
print("Return")
end

if “Return” prints then the Condition was true

You use == instead of ~=. Perhaps this is the problem, as your original code had the second.