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?
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")
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())
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.
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)