Completely stop a part of the script

Hello!

So basically my script has a detector, a tween and a result. I want it so if you jump after being detected, the script completely stops, cancels the tween and resets without reaching the result.

So how would i do that? I know how to cancel a tween but how do i stop a part of a script?

Thanks.

1 Like

Try and use a debounce!
(Might be a bit messy!)

local debounce = nil
local part = workspace.Part
local tween = "Insert tween here :)"

local function onTween() -- I like named functions because I'm weird...
	part.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player.
		if player and debounce == nil then -- Checks if the player has already hit said item.
			debounce = true
			tween:Play() -- Said tween would be a variable (local tween = {TWEEN} on line 3
			wait(1) -- Just a random time!
			debounce = nil
		end
		
	end)

end

onTween()

You can make a Touched function variable, to then call :Disconnect() on.

Here is just an experimental script to give an idea:

local part = workspace.Part

local touched = part.Touched:Connect(function(otherPart)
    print("touched")
end)

task.wait(5)

touched:Disconnect() --makes the function not work anymore
print("disconnected")
1 Like

It would be very helpful if you shared your current script!

Im having the problem with cancelling a part of the script and not detecting when a player jumps

What do you mean by part of the script?
The function?

If you want the tween to completely stop “without reaching the result”, you can call :Cancel() on the tween.

Either disable script.Enabled which will stop the code, and must be re-enabled externally (will run the script from the beginning again) or utilize the task and coroutine library.

Example script:

local function foo()
	while true do
		print("Fooing")
		task.wait(1)
	end
end

--Run foo in a new thread
local thread = task.spawn(foo) 
--Schedule it to be immediatly cancelled in 5 seconds
task.delay(5, task.cancel, thread) 

Pro tip: coroutine.running() returns the currently running thread that it was called within.
Warning: you cannot cancel a thread from within itself (but you can coroutine.yield())

yeah i want to stop the function for doing anything further

Show us the script. We can’t help you without those details.

local SPart = script.Parent
local SBox = SPart.SelectionBox
local CD = SPart.Cooldown
local Dur = SPart.Duration
local CJO = SPart.CanJumpOff

local LPlr = game:GetService("Players").LocalPlayer
local LChar = LPlr.Character or LPlr.CharacterAdded:Wait()

local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

local TGoal = {Color3 = Color3.new(255, 0, 0)}
local Tween1 = TS:Create(SBox, TweenInfo.new(Dur.Value), TGoal)

local weld

local debounce = false

return function()
	SPart.Touched:Connect(function(t)
		
		if debounce == false then
			if t.Parent:WaitForChild("HumanoidRootPart") then
				
				debounce = true
				
				weld = Instance.new("WeldConstraint")
				weld.Parent = SPart
				weld.Part0 = SPart
				weld.Part1 = t.Parent.HumanoidRootPart
				
				Tween1:Play()
				
				wait(Dur.Value)
				
				weld:Destroy()
				
				wait(CD.Value)
				
				SBox.Color3 = Color3.new(0.333333, 1, 0)
				debounce = false			
			end
		end
	end)

	
	UIS.JumpRequest:Connect(function()
		if CJO.Value == true and weld and debounce == true then
			weld:Destroy()
		end
	end)
	
end

Basically i want the tween to stop and duration to end.

(This is a module script btw)

If you want the tween to stop you can run ':Cancel()` on it. You would have to run it somewhere, I’m not exactly sure where.

you can replace the weld with Tween1:Cancel() or you can add both.

UIS.JumpRequest:Connect(function()
		if CJO.Value == true and weld and debounce == true then
            weld:Destroy()
			Tween1:Cancel()
		end
	end)

That would only cancel the tween (color change) though. The waiting for duration and cooldown to end would still exist and i need to stop it somehow.

Where is the duration and cooldown? Could you post that part here? Sorry if I’m a bit oblivious here.

debounce = true
				
weld = Instance.new("WeldConstraint")
weld.Parent = SPart
weld.Part0 = SPart
weld.Part1 = t.Parent.HumanoidRootPart
				
Tween1:Play()
				
wait(Dur.Value)
				
weld:Destroy()
				
wait(CD.Value)
				
SBox.Color3 = Color3.new(0.333333, 1, 0)
debounce = false			

try doing this then:

UIS.JumpRequest:Connect(function()
		if CJO.Value == true and weld and debounce == true then
            weld:Destroy()
			Tween1:Cancel()
            Dur.Value = 0
            CD.Value = 0

		end
	end)

Sorry for the very late reply by the way.