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?
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 Very sorry that I couldnât help.
Its ok, thanks for the help still!
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.
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
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
Ok thank you everyone who helped me. It finally work.
(Also @ChivalrousSummer Tween:Stop() doesnt work, its Tween:Cancel() lol )
Ah my bad, thank you for the reminder