How do i stop a function from continuing?

local MoveEnemy = function(Location)
	
	local Speed = script.Parent.EnemyStats.Speed.Value
	local Distance = (script.Parent.Torso.Position - Location).magnitude
	local Time = Distance/Speed
	
	local TS = game:GetService("TweenService")
	local Info = TweenInfo.new(Time,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
	local Goal = {Value = CFrame.new(Location);}
	
	local FCV = script.CFrameValue
	FCV.Value = script.Parent:GetPrimaryPartCFrame()
	
	FCV:GetPropertyChangedSignal("Value"):Connect(function()
		script.Parent:SetPrimaryPartCFrame(FCV.Value)
	end)
	
	script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value"):Connect(function()
		--stop the entire function
	end)
	
	local Tween = TS:Create(FCV,Info,Goal)
	Tween:Play()
	wait(Time)
	print("Target Reached")
end

To stop the function use return like this:

script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value"):Connect(function()
	return
end)

While return is used to return values to be used, it can also be used to stop (or break out entirely) of a function, script, or loop.

EDIT: Oh, and by the way, if you are detecting the change of a value in a value object (NumberValue, IntValue, etc.) you can use .Changed

script.Parent.EnemyStats.Speed.Changed:Connect(function()
--blah blah blah
end)

I tried that method, it still print “Target Reached” after I returned it.

EDIT: I like the method i used more lol

Hmmm, try doing something like

local e = false
script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value"):Connect(function()
	e = true
end)
if e then
   return
end

Does it still happen?

Screen Shot 2564-11-04 at 13.39.34
it still happen! maybe I used it wrong?
‘’’
local MoveEnemy = function(Location)

local Speed = script.Parent.EnemyStats.Speed.Value
local Distance = (script.Parent.Torso.Position - Location).magnitude
local Time = Distance/Speed

local TS = game:GetService("TweenService")
local Info = TweenInfo.new(Time,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
local Goal = {Value = CFrame.new(Location);}

local FCV = script.CFrameValue
FCV.Value = script.Parent:GetPrimaryPartCFrame()

FCV:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent:SetPrimaryPartCFrame(FCV.Value)
end)

local e = false
script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value"):Connect(function()
	e = true
end)

if e then
	return
end

local Tween = TS:Create(FCV,Info,Goal)
Tween:Play()
wait(Time)
print("Target Reached")

end
‘’’

Unfortunately I can’t think of any way to do this :frowning: Very sorry that I couldn’t help.

1 Like

Its ok, thanks for the help still!

1 Like

Add return wherever you want it to end. Doesn’t even need to return anything.

local MoveEnemy = function(Location)
	local endScript = false
	local Speed = script.Parent.EnemyStats.Speed.Value
	local Distance = (script.Parent.Torso.Position - Location).magnitude
	local Time = Distance/Speed

	local TS = game:GetService("TweenService")
	local Info = TweenInfo.new(Time,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
	local Goal = {Value = CFrame.new(Location);}

	local FCV = script.CFrameValue
	FCV.Value = script.Parent:GetPrimaryPartCFrame()

	FCV:GetPropertyChangedSignal("Value"):Connect(function()
		script.Parent:SetPrimaryPartCFrame(FCV.Value)
	end)

	script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value"):Connect(function()
		endScript = true
	end)
	
	if endScript then
		return
	end
	
	local Tween = TS:Create(FCV,Info,Goal)
	Tween:Play()
	wait(Time)
	print("Target Reached")
end

Thats is what he told me to try, it still print “Target Reached” which mean the function is still running

Nah, that just means:

script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value")

This event never fired. Try printing the value of “Speed.Value” every so often to see if it changes.

Screen Shot 2564-11-04 at 14.25.50
It does indeed fired. it print “ok” when the speed changes

This is a bit of a tricky situation actually, when the GetPropertyChangedSignal fires it runs a completely new thread and is not under the scope of that function so using return wouldn’t work here but you might have to resolve to an alternative to do some function <-> function operating

You can always define the Tween above the GetPropertyChangedSignal then inside the event listener you can do Tween:Cancel(); Also you don’t need to use wait() for the tween to finish you can just do Tween.Completed:Wait()

-- Might be missing code but it doesn't matter for a showcase
local Tween = TS:Create(FCV,Info,Goal)

script.Parent.EnemyStats.Speed:GetPropertyChangedSignal("Value"):Connect(function()
	Tween:Cancel()
end)

Tween:Play()
Tween.Completed:Wait()
if script.Parent:GetPrimaryPartCFrame() == Goal[Value] then
    print("Target Reached")
else
    print("Movement blocked")
end
1 Like
local f 

f = bla.bla.bla:Connect(function()
     if your functiom has done what you want then
          f:Disconnect()
    end
end)

you can also use
else return end

or you can just use coroutine.yield

2 Likes

Ok thank you everyone who helped me. It finally work.
(Also @ChivalrousSummer Tween:Stop() doesnt work, its Tween:Cancel() lol )

1 Like

Ah my bad, thank you for the reminder

1 Like